【问题标题】:Angular Material 2 Could not find core themeAngular Material 2 找不到核心主题
【发布时间】:2017-07-17 16:19:14
【问题描述】:

我知道这是一个重复的问题,但列出的解决方案都没有帮助我。我在控制台中收到以下错误: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

我已经做了什么:

还有其他几个。我似乎丢失了标签。

现在让我感兴趣的是我已经加载了从这里看到的样式。

材料检查的类也是如此:mat-theme-loaded-marker

但我似乎仍然无法加载它。同样的错误。

我的AppModule如下:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import 'hammerjs';
import { LoginComponent } from './components/login/app';
import { CookieService } from 'ngx-cookie-service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from '@angular/material';

const appRoutes: Routes = [
    {path: 'login', component: LoginComponent},
    { path: '**', component: LoginComponent }
];

@NgModule({
  declarations: [
    LoginComponent
  ],
  imports: [
    FormsModule,
    HttpModule,
    BrowserModule,
    BrowserAnimationsModule,
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    ),
    MaterialModule
  ],
  providers: [ CookieService],
  bootstrap: [ LoginComponent ]
})
export class AppModule { }

PS:我是 Angular 4 的新手。

【问题讨论】:

  • 顺便说一句:你不要再做MaterialModule了。相反,您只需包含所需组件的模块,例如 MdButtonModule 等等。看这里:Getting started.
  • stylesheets import的序列是什么?将 CSS 更新到您的帖子中
  • @pzaenger 是的,我最初尝试过,同样的错误。因此坚持这一点。
  • @Aravind 只有一个导入。没有别的了。
  • 伙计们,我让它工作了,但它看起来更像是一个黑客而不是工作。我将样式表 CSS 添加到 assets 文件夹中,将其位置添加到 angular-cli.json 中,瞧,它可以工作了。现在真正的问题是,为什么这行得通,而文档中提供的方法却行不通?

标签: css angular angular-material2


【解决方案1】:

使用材料设计开发 Angular 2,我们需要使用材料组件中的一些默认 css,否则它将在您的浏览器中显示控制台错误,例如 找不到 Angular Material 核心主题。。为了避免这些问题我们需要在你的材质组件中使用默认主题之一。从下图中你可以观察到 4 预建主题在你的 node_module.

。我正在我的 styles.css 文件中导入其中一个主题。请查看下面的屏幕截图。重新加载页面后,您可以在浏览器控制台中看到没有错误消息显示。希望对你有所帮助

【讨论】:

  • 我的设置是一样的,除了自定义样式。问题不在于 CSS 文件,它会被加载,但我猜是在材料检查之后。这是我的 CSS:imgur.com/a/WB0Xa
  • 忘了说,还是一样的错误。阅读您的回答后,我添加了body 样式。
  • 我不赞成你的回答,原因如下:正在加载 css,如相关图像中所示;上面的问题和评论中提到了导入命令,我指定它是css文件中唯一的命令。
  • 感谢您的尝试。
猜你喜欢
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多