代码之家  ›  专栏  ›  技术社区  ›  Stefan Falk

如何去除所有宿主元素的“大纲”?

  •  0
  • Stefan Falk  · 技术社区  · 6 年前

    如果有人做了

    <mat-card [routerLink]="[`item/${itemId}`]">
      <!-- Content -->
    </mat-card>
    

    卡片是可以点击的,但是卡片周围也会有一个轮廓。在我看来,我必须设置一个额外的风格

    <mat-card [routerLink]="[`item/${itemId}`]" [style.outline]="'none'">
      <!-- Content -->
    </mat-card>
    

    mat-card {
      outline: none;
    }
    

    删除此。但是,如果我创建其他组件,这会变得很乏味,因为我必须设置 outline: none; 每次我想让它从外部路由元素。

    @Component({
      selector: 'another-card',
      styles: [`
        :host {
          outline: none;
        }
      `],
      template:`
        <div>
          <!-- Content -->
        </div>
      `
    })
    export class AnotherCardComponent {
    }
    

    stackblitz

    1 回复  |  直到 6 年前
        1
  •  2
  •   Ingo Bürk    6 年前

    以下是几种不同的方法:

    1全球地

    可以全局设置样式:

    * {
      outline: none;
    }
    

    如果你只想在 routerLink

    @Directive({
      // By using the same selector, this directive is applied when
      // routerLink is used without having to add anything else
      selector: "[routerLink]",
    })
    export class RemoveOutlineDirective {
    
      @HostBinding("class.no-outline")
      public removeOutline = true;
    
    }
    

    具有

    .no-outline {
      outline: none;
    }
    

    三。外化风格

    您可以创建共享 hosts.styles.css 并将其包含在所有组件中:

    :host {
      outline: none;
    }
    
    @Component({
      selector: "app-example",
      styleUrls: ["../path/to/hosts.styles.css", "./app-example.css"],
    })
    export class AppExampleComponent {}