网站搜索

javascript - 在运行 ng add @angular/material 和 scss 时,靛蓝粉色主题会完全初始化吗?


在运行 ng add @angular/material 时,它会询问我们想要使用什么 CSS 框架和主题。我选择了 indigo-pink 和 scss。我们还需要做其他操作吗,还是 Material 组件会自动应用主题?

使用普通的 CSS(不使用 CSS 框架),我们将此添加到 styles.css:

@import “~@angular/material/prebuilt-themes/indigo-pink.css”;

我原本以为现在 Angular 会自动完成这个,因为 CSS 主题已经包含在 angular.json 中,然而我没有看到样式被自动渲染。这里是一个 stepper 的截图:

尝试将以下内容添加到 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();

更新

我生成了一个全新的项目,并尝试了上面我列出的最小设置,选择相同的选项,现在它可以正常工作了,所以我可能在某个地方犯了一个小错误。我会保留这个,以防有人遇到类似的问题。

答案#1

如果它是一个 scss 文件,可能会有区别,因为里面可能有你想在样式文件中引用的变量。由于它是一个 css 文件,唯一可能的区别只是顺序。

如果你在 styles.scss 中有规则,它们可能会被 indigo pink 样式覆盖,但这仅会在 indigo pink 在 angular.json 中的样式表之后出现时发生。