What to use in place of ::ng-deep

后端 未结 7 907
梦毁少年i
梦毁少年i 2020-11-22 10:42

I\'m trying to style an element placed by the router outlet in angular and want to make sure that the element generated gets a width of 100%

From most of the replies

7条回答
  •  情书的邮戳
    2020-11-22 11:23

    To bypass the deprecated ::ng-deep, I usually disable ViewEncapsulation. Although this is not the best approach, it has served me well.

    To disable ViewEncapsulation, do the following in your component:

    import { Component, ViewEncapsulation } from '@angular/core';
    
    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.scss'],
      encapsulation: ViewEncapsulation.None
    })
    
    export class HeaderComponent {
    
    }
    

    This will make the .scss styles in this component global to the whole application. To not allow the styles to go up the chain to parent and sibling components, wrap the whole scss with the selector like so:

    app-header {
      // your styles here and any child component styles can go here
    }
    

    Now, the styles specified here will go down to children components so you have to be extra specific with your css selectors and mind your p's and q's when adding CSS (maybe add the child selector specified in your Angular app and then its styles).

    I say it is not the best approach because of the paragraph above, but this has served me well.

提交回复
热议问题