模板分析错误:'mat-sidenav-container'不是已知元素

时间:2018-01-08 05:00:03

标签: angularjs

我是Angular / material2的新手,我不确定在尝试按照sidenav上的文档时我错过了什么。

在我的AppComponent中,我想显示一个sidenav,我已经实现如下:

app.component.tss

import {
    NgModule,
    Component
} from '@angular/core';
import {
    MatSidenavModule,
    MatSidenavContent,
    MatSidenav
} from '@angular/material';
import {
    BrowserAnimationsModule
} from '@angular/platform-browser/animations';

@NgModule({
    imports: [
        BrowserAnimationsModule,
        MatSidenav
    ],
    exports: [
        BrowserAnimationsModule,
        MatSidenav
    ]
})
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'app';
}

app.component.html

<!--The content below is only a placeholder and can be replaced.-->
<mat-sidenav-container>

</mat-sidenav-container>
<div style="text-align:center">
    <h1>
        Welcome to {{ title }}!
    </h1>
    <nav>
        <a routerLink="/clients">Clients</a>
    </nav>
    <router-outlet>

    </router-outlet>

</div>]

错误是:

Uncaught Error: Template parse errors:
'mat-sidenav-container' is not a known element:
1. If 'mat-sidenav-container' is an Angular component, then verify that it is part of this module.
2. If 'mat-sidenav-container' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message

我的代码中缺少什么?

3 个答案:

答案 0 :(得分:4)

您需要在app.module.ts中导入 import { MaterialModule } from './material.module';

imports: [    
    MaterialModule 
  ],

答案 1 :(得分:3)

Angular 9实现

import { MatSidenavModule } from '@angular/material/sidenav';

imports: [MatSidenavModule]

答案 2 :(得分:2)

Sajeetharan的答案不适用于较新版本的角形材料。

来自here

  

MaterialModule在2.0.0-beta.3版本中已弃用,它是   在2.0.0-beta.11版本中完全删除...

所以,要解决此问题:

在2.0.0-beta.3上

import {
  MatSidenavModule,
} from '@angular/material';

@NgModule({
  exports: [MatSidenavModule],
  imports: [
    MatSidenavModule
  ],
})

以及旧版本

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