当运行ng add @angular/material
时,它会询问我们想要什么CSS框架和主题。我选择了靛蓝粉色和scss。我们需要做其他事情吗,或者 Material 组件只是自动应用主题吗?
使用普通 css(无 CSS 框架),我们将其添加到 styles.css
:
@import “~@angular/material/prebuilt-themes/indigo-pink.css”;
我假设 Angular 现在会自动执行此操作,因为 CSS 主题包含在 angular.json
中,但是我没有看到样式自动渲染。这是步进器的屏幕截图:
尝试将以下内容添加到styles.scss
,但错误仍然存在:
/* You can add global styles to this file, and also import other style files */
@import '~@angular/material/theming';
// Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core();
更新
我生成了一个全新的元素,并使用相同的选择尝试了上面的最小设置,现在它可以工作了,所以我可能在某个地方犯了一个小错误。如果有人遇到类似的问题,我会保留此内容。
最佳答案
如果它是一个 scss 文件,则可能会有所不同,因为其中可能有您想要在样式文件中引用的变量。由于它是一个 css 文件,唯一的区别可能是顺序。
如果您在 styles.scss 中有规则,它们可能会被靛蓝粉红色样式覆盖,但只有当靛蓝粉红色出现在 angular.json 中的样式表之后时,才会发生这种情况。
关于javascript - 运行 ng add @angular/material 和 scss 时,靛蓝粉色主题是否完全初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57517161/