【问题标题】:Angular 2 Material Custom ThemesAngular 2 材质自定义主题
【发布时间】:2018-01-02 17:30:08
【问题描述】:

我正在使用 Angular 2 和 Angular Material,我需要创建一个自定义主题,以便为组件使用自定义颜色。我关注了angular docs,但我无法让它运行。

到目前为止我做了什么:

1.我创建了一个文件my-theme.scss:

@import "../node_module/@angular/material/_theming";

@include mat-core();

$my-theme-primary: mat-palette($mat-amber);
$my-theme-accent: mat-palette($mat-orange, A200, A100, A400);
$my-theme-warn: mat-palette($mat-red);

$my-theme: mat-light-theme($my-theme-primary, $my-theme-accent, $my-theme-warn);

@include angular-material-theme($my-theme);

2.我将创建的SASS文件导入到styles.css中

@import "my-theme.scss"

3.我在样式部分的.angular-cli.json文件中引用了它:

"styles": [
   "styles.css",
   "my-theme.scss"
]

我在 localhost 下的网站告诉我“编译失败”模块未找到:'../node_modules/@angular/material/_theming'。

我尝试了几种导入路径的变体(“~@angular/material/_theming”、“../node_modules/@angular/material/_theming”、“~@angular/material/theming”、“../ node_modules/@angular/material/theming"...) 在 my-theme.scss 文件中。在 Angular Docs 中,他们使用“theming”作为位置,但我在某处读到,Angular Material 将文件转移到“_theming”,我也在我的 folder 结构中找到了它。

当我使用路径“../node_modules/@angular/material/_theming.scss”时,编译器会找到该文件,但由于另一个错误导致编译失败:

    Module build failed: Missed semicolon (30:1)  
  28 | // Media queries  
  29 | // TODO: Find a way to respect media query ranges. 
> 30 | // TODO: For example the xs-breakpoint should not interfere with the sm-breakpoint.     
     | ^  
  31 | $mat-xsmall: 'max-width: 600px';  
  32 | $mat-small: 'max-width: 960px';

这对我来说似乎很奇怪,因为 _theming.scss 文件是 Angular Material 文件,错误的位置是注释。我也没有在网络上的其他工作实现中看到任何以 .scss 结尾的导入语句。

我花了几个小时搜索如何将自定义主题添加到我的网站...我错过了什么吗?我对 Angular 2 和 SASS 很陌生。

PS:我已经有点困惑了,是否应该将 my-theme.scss 导入到 styles.css 中。 Angular Docs 对此只字未提,但我读了一些 cmets,人们在其中指出,这解决了他们使用自定义 Angular Material 主题的问题。

项目结构:

my-project
 |--node_modules
 | |--@angular
 |   |--material
 |     |--prebuild-themes
 |     |--_theming.scss
 |--src
 | |--app
 | | |--my-component.css | html | ts
 | | |--app.module.ts
 | |--assets
 | |--index.html
 | |--main.ts
 | |--my-theme.scss
 | |--styles.css
 |--.angular-cli.json

package.json 文件:

  "dependencies": {
    "@angular/animations": "^4.3.1",
    "@angular/cdk": "^2.0.0-beta.8",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/material": "^2.0.0-beta.8",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "rxjs": "^5.1.0",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.2.1",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }

Windows 7、64 位 火狐 52.2.1 Visual Studio 代码 1.14.1

【问题讨论】:

  • 我遇到了同样的问题。从@Faisal 的答案中,我将 style.css 更改为 style.scss 并在 .angular-cli.json 中进行了相同的更改并且它有效。您不需要在 .angular-cli.json 中包含主题 scss 文件。注意,我不需要在主题 scss 文件名的开头添加下划线。

标签: angular angular-material angular-material2


【解决方案1】:

这会有所帮助:

  1. 将您的主题文件名更改为_my-theme.scss
  2. 将您的 styles.css 更改为 styles.scss
  3. 将您的主题导入styles.scss 文件,例如:

    @import './path/to/your/theme/my-theme'; // 你不需要下划线和文件 在这里扩展

  4. 您不需要在styles: []中包含您的主题文件

【讨论】:

  • 谢谢,您的回答和this one 帮助了我。为了解决这个问题,我使用 CLI 命令 ng new my-project --style=scss 创建了一个新的 Angular 项目(当然我也需要使用 npm installall commands to setup Angular 2 Material)。然后我添加了 _my-theme.scss 文件(参见SASS Partials)并将@import "my-theme" 添加到styles.scss(两个文件都在同一个文件夹中)。
  • 很高兴它有帮助!干杯(Y)
  • 设置新项目后,我的 angular-cli.json 文件定义了"styles": ["styles.scss"]"defaults": {"styleExt": "scss", "component": {} }。手动更改两个变量在旧项目中不起作用,这就是为什么我决定制作一个支持 scss 的全新项目并将我的组件和其他内容复制到新项目中。
  • 请记住,当您的组件样式在 scss 文件中定义时,您只需将“styleExt”更改为“scss”。这也应该手动工作,我在我的一个项目中做过同样的事情。
  • 我刚刚在同一个问题上花了几个小时。我尝试手动将 .css 文件更新为 .scss 并且没有任何运气。使用--style=scss 标志修复了问题,从头开始重新创建整个项目。
【解决方案2】:

您可能需要更改您的应用以使用 sass。在您的 angular-cli 文件中更改以下行:

"styleExt": "css",

收件人:

"styleExt": "scss",

然后将所有 css 文件重命名为 scss。 把你的 my themse.scss 放在那里。

重命名后也要在 cli 上更改

"styles": [
   "styles.scss",
   remove this line =>"my-theme.scss"
]

然后在 assets 文件夹上创建一个名为 styles 的文件夹。

应该是这样的

|--assets
   |--styles
      |--_my-theme.scss

然后在 style.scss 文件中像这样导入它:

@import "~assets/styles/_my-theme.scss";

【讨论】:

  • 感谢您的回答。不幸的是,它对我不起作用,但也许我误解了一些东西。
  • 我将 "styleExt": "css" 更改为 "scss",从 angular-cli 文件中的 "styles" 中删除了 "my-theme.scss" 引用,调整了文件夹结构和导入陈述。 “把你的 theme.scss 放在那里”是什么意思?我应该把 theme.scss 文件放在哪里?
  • 构建现在失败:“./src/app/app.component.scss 模块图片失败:错误:ENOENT:没有这样的文件或目录,scandir 'C:\angularworkspace\kundenportal\ node_module\node-sass\vendor'" 然后是堆栈跟踪,最后是 "multi webpack-dev-server/client?http:localhost:4200 ./src/main.ts'
  • 如果你正确使用 sass,你可以在 style.scss 文件中导入你的主题和一般样式,在你的 angular-cli 文件中只有 style.scs。
  • 你的样式应该是这样的 -> assets>styles>theme.scss
猜你喜欢
  • 2019-06-11
  • 2019-07-06
  • 2018-10-08
  • 2017-08-04
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 2021-02-27
相关资源
最近更新 更多