ViewEncapsulation

Defines template and style encapsulation options available for Component's Component.

查看"说明"...

      
      enum ViewEncapsulation {
  Emulated: 0
  Native: 1
  None: 2
  ShadowDom: 3
}
    

说明

See encapsulation.

成员列表

成员说明
Emulated: 0

Emulate Native scoping of styles by adding an attribute containing surrogate id to the Host Element and pre-processing the style rules provided via styles or styleUrls, and adding the new Host Element attribute to all selectors.

This is the default option.

Native: 1
None: 2

Don't provide any template or style encapsulation.

ShadowDom: 3

Use Shadow DOM to encapsulate styles.

For the DOM this means using modern Shadow DOM and creating a ShadowRoot for Component's Host Element.

使用说明

Example

@Component({ selector: 'my-app', template: ` <h1>Hello World!</h1> <span class="red">Shadow DOM Rocks!</span> `, styles: [` :host { display: block; border: 1px solid black; } h1 { color: blue; } .red { background-color: red; } `], encapsulation: ViewEncapsulation.ShadowDom }) class MyApp { }
      
      
  1. @Component({
  2. selector: 'my-app',
  3. template: `
  4. <h1>Hello World!</h1>
  5. <span class="red">Shadow DOM Rocks!</span>
  6. `,
  7. styles: [`
  8. :host {
  9. display: block;
  10. border: 1px solid black;
  11. }
  12. h1 {
  13. color: blue;
  14. }
  15. .red {
  16. background-color: red;
  17. }
  18.  
  19. `],
  20. encapsulation: ViewEncapsulation.ShadowDom
  21. })
  22. class MyApp {
  23. }