Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the error “Could not find Angular Material core theme” in Angular 2?

I am practically new in Angular2 and inherited the project with Angular 2 frontend. After switching to the new Angular 4.0 on console I see the error:

"compatibility.ts:221 Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.angular.io/guide/theming"

The mdb.min.css is in the app/assets/css folder. The specification in package.json shows the following:

"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
...
"angular-bootstrap-md": "*"

How to fix the error?

like image 525
Alex Avatar asked Aug 18 '17 16:08

Alex


People also ask

How to add a candy theme in Angular 2?

Look here for information: Angular Material 2 Theming instructions. So, in the .angular-cli.json, under the styles property, add "themes/candy.scss", next to the "themes/styles.css". I have a folder in my projects called "themes". I put the styles.css and the candy.scss in the themes folder. Now, Angular-CLI can find your theme.

How do I start using Angular Material?

For existing apps, follow these steps to begin using Angular Material. You can use either the npm or yarn command-line tool to install packages. Use whichever is appropriate for your project in the examples below. A snapshot build with the latest changes from master is also available.

Should I import ngmodules before or after browsermodule?

Whichever approach you use, be sure to import the Angular Material modules after Angular's BrowserModule, as the import order matters for NgModules. Including a theme is required to apply all of the core and theme styles to your application.

Is it possible to use animations in @angular/animations?

Note: @angular/animations uses the WebAnimation API that isn't supported by all browsers yet. If you want to support Material component animations in these browsers, you'll have to include a polyfill. If you don't want to add another dependency to your project, you can use the NoopAnimationsModule.


2 Answers

You have to import a theme into your global css or sass file:

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

or alternatively include it in your index.html file:

<link href="node_modules/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet">

As in the theming documentation already mentioned Angular Material provides the following pre-built themes:

  • deeppurple-amber.css
  • indigo-pink.css
  • pink-bluegrey.css
  • purple-green.css

You have to include one of these themes or you create your own custom theme.

like image 169
Philipp Kief Avatar answered Nov 11 '22 13:11

Philipp Kief


Adding @import to styles.css doesn't work for me, adding theme to angular.json did the trick

You can replace the default angular-material-theme with one of the above-mentioned angular-material-themes using the following ways.

You can directly include the pre-built angular-material-theme in the styles.css file.

@import '@angular/material/prebuilt-themes/deeppurple-amber.css';

Or Add the following inside the head tag of index.html file.

<link href="node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css" rel="stylesheet">

Or Update the following in angular.json file.

"styles": [
{
"input": "node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css"
},
"src/styles.css"
],
like image 24
Masoud Bimmer Avatar answered Nov 11 '22 13:11

Masoud Bimmer