确认应用中是否正确导入了 Angular Material 主题文件。根据需要,在angular.json
文件中添加主题样式文件或在index.html
文件中添加其CDN链接。
angular.json
中添加以下内容:
"styles": [
"node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.css"
]
index.html
中添加以下链接:
确认是否正确设置材料风格到 Angular 组件。@Component
装饰器应该包含encapsulation
属性,并使用ViewEncapsulation.None
。
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
encapsulation: ViewEncapsulation.None
})
若要修改 Angular 材料风格,应在组件文件中覆盖默认样式表并添加自定义样式。在样式表中,使用 CSS 中的选择器和属性覆盖默认样式表。
.mat-card {
background-color: #333;
color: #fff;
}
如需更改特定组件的默认属性,请使用组件中的提供者和自定义类。 例如,将mat-button
的默认矩形形状更改为圆形。
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css'],
providers: [{ provide: MAT_BUTTON_TOGGLE_DEFAULT_OPTIONS, useValue: { appearance: 'outline' } }],
viewProviders: [{ provide: MAT_ICON_REGISTRY, useClass: CustomIconRegistry }]