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

淺唱寂寞╮ 提交于 2019-12-05 05:53:07

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.

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"
],

I had same issue when i upgraded from Angular version 7 to 8.Even after imported to .sass file / index.html / angular.json file.I tried upgrading angular material separately again , it solved the issue.

ng update @angular/material@latest

Please insert below code into your styles.css or styles.scss which is located in your src folder.

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

You can select any css under the prebuilt-themes folder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!