繁体   English   中英

Angular2 材质“md-icon”不是已知元素

[英]Angular2 material 'md-icon' is not a known element

我有使用@angular2-material 2.0.0-alpha.8-2 版本的angular2 应用程序。 一切正常。 现在我决定将材料版本升级到最新版本,即 2.0.0-alpha.9-3。 我按照GETTING_STARTED 中提到的步骤进行操作 早些时候,我导入了以下单个模块:

@NgModule({
imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule,

    MdIconModule,
    MdButtonModule,
    MdCardModule,
    MdCheckboxModule,

    ....
    ....  

但是 2.0.0-alpha.9-3 版本的更改日志说:

“Angular Material 已从 @angular2-material/... 包更改为 @angular/material 下的单个包。伴随此更改,还有一个新的 NgModule MaterialModule ,它包含所有组件。”

所以我删除了明确导入的材料模块并将其更改为:

@NgModule({
imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule,

    MaterialModule.forRoot(),
    ....
    ....

在此更改后,我收到以下错误

'md-icon' 不是已知元素:

  1. 如果 'md-icon' 是一个 Angular 组件,那么验证它是否是这个模块的一部分。
  2. 如果 'md-icon' 是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。

我是否需要显式导入单个模块或如更改日志中提到的那样 MaterialModule 包含所有组件,我不应该显式导入单个模块? 如果我不应该导入单个模块,那么错误的根源是什么?

export部分呢? 你在那里提供MaterialModule吗?

@NgModule({
  imports: [
    MaterialModule.forRoot()
  ],
  exports: [
    MaterialModule
  ]
})

请记住在 index.html 中提供图标样式:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

之后,您应该能够在视图中使用图标:

<md-icon>delete</md-icon>

您需要在app.module.ts中导入MatIconModule并将其添加到同一文件中的导入数组中。

像这样例如:

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { TreeModule } from "angular-tree-component";
import { HttpClientModule } from "@angular/common/http";

import { MatButtonModule } from "@angular/material/button";
import { MatIconModule } from "@angular/material/icon"; // <----- Here

import { EclassService } from "./services/eclass.service";

import { AppComponent } from "./app.component";
import { FormsModule } from "@angular/forms";
import { AsyncTreeComponent } from "./components/async-tree/async-tree.component";


@NgModule({
  declarations: [
    AppComponent,
    AsyncTreeComponent
  ],
  imports: [
    BrowserModule, BrowserAnimationsModule, FormsModule, TreeModule, HttpClientModule, MatButtonModule, MatIconModule // <--- HERE
  ],
  providers: [EclassService],
  bootstrap: [AppComponent]
})
export class AppModule { }

如果您加载这样的子模块:

{path: 'childModule', loadChildren: 'app/child/child.module#childModule'}

然后在子模块中,您必须再次导入 MaterialModule。 例如

@NgModule({
    imports: [
        sharedModules,
        childRouting,
        MaterialModule.forRoot()
    ],
    declarations: [
    ],
    providers: []
})
export class childModule {
}

添加

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

index.html

<i class="material-icons">delete</i>而不是<md-icon>delete</md-icon>

使用 mat-icon 的两个步骤:

  1. 将此插入到 index.html 文件中。

     <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  2. 将此行插入 app.module.ts 文件中。 如果执行延迟加载,则将其包含在该模块或 sharedModule 中。

     import { MatIconModule } from '@angular/material/icon';

解决方案是添加 md-icon、md-input 等模块和样式表。

您还需要在 app.module.ts 中添加 CUSTOM_ELEMENTS_SCHEMA,如下所示:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

然后添加

providers: [],
bootstrap: [AppComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]

将此添加到 index.html:

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

并添加到 app.module.ts 文件中:

import {
  MatIconModule
} from '@angular/material';

imports: [MatIconModule]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM