关于您在测试组件中面临的宽度问题,假设您希望整个元素的宽度达到 350px,您应该将其 display 属性定义为 block:
:host {
background: blue;
display: block;
}
自定义元素没有默认的显示属性,您的浏览器无法猜测您的意思。
关于应用.test
组件上的样式,app-component
样式是使用封装的_nghost-c0
and _ng-content-c0
属性,因此不应用于test.component
。如果您想应用.test
在其他组件上,您可以编写CSS RULE在全球范围内styles.css
全局应用的文件:
//styles.css
.test {
background-color: red;
width: 350px;
}
或使用viewEncapsulation
您的财产@component
装饰器是这样的:
//app.component.ts
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None
})
然后你可以离开CSS RULE on the app.component
file
//app.component.css
.test {
background-color: red;
width: 350px;
}
但现在该文件中的样式没有被封装,并且会渗透到其他组件。