【发布时间】:2021-09-04 05:07:05
【问题描述】:
我读到ShadowDOM 将 shadow dom 树与外部 css 隔离开来,所以我尝试使用 ViewEncapsulation.ShadowDom 在 Angular 中使用它,但似乎全局 css 仍在泄漏到 Shadow DOM .
请在此处查看代码: https://stackblitz.com/edit/shadow-dom-test?file=src/app/app.component.ts
import { Component, VERSION, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ],
encapsulation: ViewEncapsulation.ShadowDom
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
}
之后,在style.css 中设置任何css 都会影响AppComponent 下的元素,即使封装设置为ViewEncapsulation.ShadowDom 并且组件包含在ShadowDom 树下。
见下图,ShadowDOM下的元素继承Font-size
【问题讨论】:
-
您使用的是哪种浏览器?
-
some css是什么?你能具体说明问题吗? -
@deepakchethan 我正在使用 google-chrome
-
@yurzui,是的,style.css 下的全局样式会影响
AppComponent下的元素,即使它有ViewEncapsulation.ShadowDom。我一直认为全局样式不会影响 ShadowDom 下的元素。
标签: angular shadow-dom