Can some one explain what's the difference between ViewEncapsulation.Native, ViewEncapsulation.None and ViewEncapsulation.Emulated in angular2.
I tried to google it and read some articles, but I'm not able to understand the difference.
Below I have two components Home (home.ts) i.e. parent component and MyComp (my-comp.ts). I want to define styles in the parent that are being used in the child component.
Should I use ViewEncapsulation.Native or ViewEncapsulation.None
home.ts
import {Component, ViewEncapsulation} from 'angular2/core'; import {MyComp} from './my-comp'; @Component({ selector: 'home', // <home></home> providers: [ ], directives: [ MyComp ], styles: [` .parent-comp-width { height: 300px; width: 300px; border: 1px solid black; } `], template:` <my-comp></my-comp> <div class="parent-comp-width"></div> `, encapsulation: ViewEncapsulation.Native }) export class Home { }
my-comp.ts
import {Component} from 'angular2/core'; @Component({ selector: 'my-comp', // <home></home> template: ` <div class="parent-comp-width">my-comp</div> ` }) export class MyComp { }
ViewEncapsulation.Emulated. Angular modifies the component's CSS selectors so that they are only applied to the component's view and do not affect other elements in the application (emulating Shadow DOM behavior). For more details, see Inspecting generated CSS.
In the browser, when you examine source code, you will a Shadow DOM has been created for the AppComponent and the style is scoped to that. Therefore, in ViewEncapsulation. Native , Angular creates a Shadow DOM and the style is scoped to that Shadow DOM.
View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa. Angular provides three encapsulation strategies: Emulated (default) - styles from main HTML propagate to the component.
update If you want styles that are added to Parent
applied to Child
you need to set ViewEncapsulation.None
in the Child
component so it doesn't prevent styles to bleed in.
Emulated
and Native
are just two different ways to prevent styles to bleed in to and out from components. None
is the only one that allows styles to cross component boundaries.
original
ViewEncapsulation.None is simple no encapsulation
ViewEncapsulation.Emulated (currently the default in Angular2)
adds attributes to component tags and child elements and manipulates the CSS (adding the attributes to the selectors) added to the page so the styles don't bleed into each other - to keep styles scoped to the components where they are added even though the styles are all added collected in the head of the page when components are loaded.
ViewEncapsulation.Native creates custom elements with shadow DOM where the browsers native implementation ensures the style scoping.
If the browser doesn't support shadow DOM natively, the web-components polyfills are required to shim the behavior. This is similar to ViewEncapsulation.Emulated
but the polyfills are more expensive because they polyfill lots of browser APIs even when most of them are never used. Angulars Emulated
emulation just adds the cost for what it uses and is therefore much more efficient for Angular applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With