components: Error Setting Up Custom Material Theme in Angular-CLI Project
My development environment: /cli: 1.0.0-beta.31 node: 7.4.0 os: darwin x64
I am using material2 elements throughout my Angular 2 app, but would now like to be able to control the colors used for those elements. To do so I am trying to set up a custom theme to use. So far, no go. My understanding is I need to do the following:
1.) Create a new file at the src level of the app. I have done that, and have a file titled “custom_theme.scss” that looks like this:
@import '~@angular/material/core/theming/all-theme';
@import 'sass-material-colors-classes';
@include md-core();
// Customizations go here, for instance:
$app-primary: md-palette($md-indigo);
$app-accent: md-palette($md-green, 500);
$app-warn: md-palette($md-red);
// Create the theme object (a Sass map containing all of the palettes).
$app-theme: md-light-theme($app-primary, $app-accent, $app-warn);
@include angular-material-theme($app-theme);
I am then including that as an import in my src level “styles.css” file, like this:
@import '~@angular/material/core/theming/prebuilt/deeppurple-amber.css';
@import 'custom_theme.scss';
When I then try and boot up the app, I get a compilation error:
ERROR in ./~/css-loader?{“sourceMap”:false}!./src/custom_theme.scss Module not found: Error: Can’t resolve ‘@angular/material/core/theming/all-theme’ in ‘/Users/dg/Documents/abc/kra/cli-ark/src’ @ ./~/css-loader?{“sourceMap”:false}!./src/custom_theme.scss 3:10-123 @ ./~/css-loader?{“sourceMap”:false}!./~/postcss-loader!./src/styles.css @ ./src/styles.css @ multi ./src/styles.css
Not sure what’s going on here. Some additional info, I’m actually using LESS in this app. But according to the official docs, this shouldn’t be a problem - as it states that one doesn’t need to be using scss throughout the app, it’s just this one file that needs to be scss. And, because this is an Angular-CLI project, it should compile automatically.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (2 by maintainers)
Had the same issue with CLI: 1.6.4 node: 9.2.0 Angular: 5.0.0 Material: 5.0.4
just need to remove the
~
at the'~@angular/material/prebuilt-themes/pink-bluegrey.css';
Maybe it’s just me, but I had to restart the
ng serve
to get mine working. Spent more time than I’d like to admit before I figured that out 😟As @VRedondo said, removing the ~ at the ‘~@angular/material/prebuilt-themes/pink-bluegrey.css’ fixed this issue.
The question is why??? Documentation says to use ~.
I found many issues on github about importing prebuilt-themes, but most of them are closed as fixed, however this bug is still there. angular’s team should investigate issues like this one, because they are really boring.
In my case, at some point my app broke after I upgraded from Angular 2 to Angular 4. After reading @tschnoelzer’s comment, all I needed to do was change my import in my styles.css file from
~@angular/material/core/theming/prebuilt/pink-bluegrey.css
to'~@angular/material/prebuilt-themes/pink-bluegrey.css';
No edits to angular-cli.json file were needed.
I was following the other issue thread(#2277) and tried some solutions, but until now, nothing.
I followed the docs @kara referenced, but it stills returns the following error:
My setup is like this: TLDR; ~I’m using
@import "./../node_modules/@angular/material/theming";
because with~@angular
sintax didn’t found the file.~ I’m using~@angular/material/_theming.scss
due to an issue with a new version (6.0.4) of sass-loader (see #4449 ), but it returns the error above. I’m using@angular/cli 1.0.1
and@angular/material 2.0.0-beta.3
.Edit: Thanks to @willshowell for the heads up .
src/theme.scss
package.json
angular-cli.json
I was also facing this … as i also import @import ‘~https://fonts.googleapis.com/icon?family=Material+Icons’; using font awesome I had to follow agusdutras hint and use
styles.css for fonts ( above) and styles.scss for material theme, also had to change URL to @import ‘~@angular/material/prebuilt-themes/deeppurple-amber.css’;
using this in cli config:
“styles”: [ “styles.scss”, “styles.css”, “…/node_modules/font-awesome/css/font-awesome.css” ],
@CoreyCole I’m ashamed to say… the same thing happened to me 😃
I was facing the same issue.
I just changed ‘style.css’ to ‘style.scss’, updated it reference on the styles section of .angular-cli.json and it started working with the template improted just like the one in the material.angular.io example.
No need to include ‘custom_theme.scss’ on the angular-cli.
hope it helps.
@darrenbrett You’ll want to do two things:
See more here: https://github.com/angular/material2/blob/master/guides/theming.md