[英]Angular Material Modules are not recognized
我在這個錯誤上浪費了很多時間,我正在開始新的angular5項目,我想使用角度材料。 我在material.module.ts文件中導入和導出了材料模塊:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { MatIconModule,MatButtonModule,MatSidenavModule,MatToolbarModule } from '@angular/material';
@NgModule({
imports: [
BrowserModule,
CommonModule,
MatIconModule,
MatButtonModule,
MatSidenavModule,
MatToolbarModule,
],
exports: [
MatIconModule,
MatButtonModule,
MatSidenavModule,
MatToolbarModule
],
declarations: []
})
export class MaterialModule { }
然后我將material.module導入了app.module中:
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { FlexLayoutModule } from "@angular/flex-layout";
import { MasterPageModule } from './master-page/master-page.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialModule } from './material.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
CommonModule,
AppRoutingModule,
FormsModule,
HttpModule,
BrowserAnimationsModule,
FlexLayoutModule,
MasterPageModule,
MaterialModule
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
當我也嘗試使用<mat-toolbar> </mat-toolbar> I
出現了常見錯誤:
Template parse errors:'mat-toolbar' is not a known element:
1. If 'mat-toolbar' is an Angular component, then verify that it is part of this module.
2. If 'mat-toolbar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
更新包.Json
{
"name": "front-end-app",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.9",
"@angular/cdk": "^5.2.4",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/flex-layout": "^5.0.0-beta.14",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/material": "^5.2.4",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"rxjs": "^5.5.6",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "~1.7.4",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
}
我看不出哪里有錯! 任何幫助將不勝感激。
您必須在使用材料組件的每個模塊中導入MaterialModule
。
這意味着,如果MasterPageModule
中的組件之一使用<mat-toolbar>
,則還必須將材料模塊導入該模塊中。 僅將其導入app.module
中是app.module
。
也不要在您的MaterialModule
導入BrowserModule
。 而是導入CommonModule
。 請參閱文檔以獲取詳細說明。
不要在任何其他模塊中導入BrowserModule。 功能模塊和延遲加載的模塊應改為導入CommonModule。 他們需要通用指令。 他們不需要重新安裝應用程序范圍的提供程序。
請參閱文檔以獲取詳細說明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.