模板解析错误:“ mat-sidenav-container”不是已知元素

New*_*wtt 5 angular

我是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';
}
Run Code Online (Sandbox Code Playgroud)

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>]
Run Code Online (Sandbox Code Playgroud)

错误是:

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
Run Code Online (Sandbox Code Playgroud)

我的代码中缺少什么?

Ale*_*aut 6

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

这里

在2.0.0-beta.3版本中不推荐使用MaterialModule,在2.0.0-beta.11版本中将其完全删除...

因此,要解决此问题:

在2.0.0-beta.3之后

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

@NgModule({
  exports: [MatSidenavModule],
  imports: [
    MatSidenavModule
  ],
})
Run Code Online (Sandbox Code Playgroud)

在旧版本中

@NgModule({
  imports: [
    MaterialModule
  ],
})
Run Code Online (Sandbox Code Playgroud)


Far*_*rel 6

Angular 9 实现

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

imports: [MatSidenavModule]
Run Code Online (Sandbox Code Playgroud)


Saj*_*ran 5

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

imports: [    
    MaterialModule 
  ],
Run Code Online (Sandbox Code Playgroud)