【发布时间】:2016-06-09 16:26:12
【问题描述】:
谁能解释一下Angular2中的ViewEncapsulation.Native、 ViewEncapsulation.None和ViewEncapsulation.Emulated有什么区别强>。
我尝试用谷歌搜索并阅读了一些文章,但我无法理解其中的区别。
下面我有两个组件 Home (home.ts),即父组件和 MyComp (my-comp.ts)。我想在父组件中定义子组件中使用的样式。
我应该使用 ViewEncapsulation.Native 还是 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.None。至少在你知道你需要别人之前(; -
是的,如果您不关心视图封装,只需将其设置为
None在任何地方,那么您就有了在 Web 组件前时代常见的行为。
标签: typescript angular