简体   繁体   English

mat-toolbar,mat-sidenav和mat-sidenav-container未知

[英]mat-toolbar, mat-sidenav and mat-sidenav-container is not a known element

I am trying to use mat-toolbar but am getting error: 我正在尝试使用mat-toolbar,但出现错误:

mat-menu.component.html : mat-menu.component.html

<mat-toolbar color="primary">

  <span>Responsive Navigation</span>

  <span class="example-spacer"></span>

  <div fxShow="true" fxHide.lt-md="true">

    <!-- The following menu items will be hidden on both SM and XS screen sizes -->

    <a href="#" mat-button>Menu Item 1</a>

    <a href="#" mat-button>Menu Item 2</a>

    <a href="#" mat-button>Menu Item 3</a>

    <a href="#" mat-button>Menu Item 4</a>

    <a href="#" mat-button>Menu Item 5</a>

    <a href="#" mat-button>Menu Item 6</a>

  </div>

  <div fxShow="true" fxHide.gt-sm="true">

    <a href="#" (click)="sidenav.toggle()">Show Side Menu</a>

  </div>

</mat-toolbar>

<mat-sidenav-container fxFlexFill class="example-container">

  <mat-sidenav #sidenav fxLayout="column">

    <div fxLayout="column">

      <a (click)="sidenav.toggle()" href="#" mat-button>Close</a>

      <a href="#" mat-button>Menu Item 1</a>

      <a href="#" mat-button>Menu Item 2</a>

      <a href="#" mat-button>Menu Item 3</a>

      <a href="#" mat-button>Menu Item 4</a>

      <a href="#" mat-button>Menu Item 5</a>

      <a href="#" mat-button>Menu Item 6</a>

    </div>

  </mat-sidenav>

  <!--<mat-sidenav-content fxFlexFill>Main content</mat-sidenav-content>-->

</mat-sidenav-container>

mat-menu.component.ts : mat-menu.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-mat-menu',
  templateUrl: './mat-menu.component.html',
  styleUrls: ['./mat-menu.component.scss']
})
export class MatMenuComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

I have imported below in my app.module.ts: I believe I have imported all the required modules required but am not sure why angular does not identifies mat-toolbar and other components 我已在app.module.ts中导入以下内容:我相信已导入所需的所有必需模块,但不确定为什么angular无法识别出垫工具栏和其他组件

import {MatToolbarModule} from '@angular/material/toolbar';
import { MatMenuComponent } from '../../components/mat-menu/mat-menu.component';
import {
  MatAutocompleteModule,MatButtonModule,MatButtonToggleModule,MatCardModule,
  MatCheckboxModule,MatChipsModule,MatDatepickerModule,MatDialogModule,MatDividerModule,MatExpansionModule,
  MatGridListModule,  MatIconModule,  MatInputModule,  MatListModule,  MatMenuModule,  MatNativeDateModule,  MatPaginatorModule,  MatProgressBarModule,  MatProgressSpinnerModule,
  MatRadioModule,MatRippleModule, MatSidenavModule, MatSliderModule, MatSlideToggleModule,MatSnackBarModule, MatSortModule,MatStepperModule,  MatTooltipModule,
} from '@angular/material';

@NgModule({
  exports: [
    MatAutocompleteModule,
    MatButtonModule,
    MatButtonToggleModule,
    MatCardModule,
    MatCheckboxModule,
    MatChipsModule,
    MatStepperModule,
    MatDatepickerModule,
    MatDialogModule,
    MatDividerModule,
    MatExpansionModule,
    MatGridListModule,
    MatIconModule,
    MatInputModule,
    MatListModule,
    MatMenuModule,
    MatNativeDateModule,
    MatPaginatorModule,
    MatProgressBarModule,
    MatProgressSpinnerModule,
    MatRadioModule,
    MatRippleModule,
    MatSelectModule,
    MatSidenavModule,
    MatSliderModule,
    MatSlideToggleModule,
    MatSnackBarModule,
    MatSortModule,
    MatTableModule,
    MatTabsModule,
    MatToolbarModule,
    MatTooltipModule,
  ],
  imports: [
    CommonModule,
    RouterModule.forChild(AdminLayoutRoutes),
    FormsModule,
    ReactiveFormsModule,
    MatTabsModule,
    MatSelectModule,
    MatTableModule,
    MatToolbarModule,
    BrowserModule,
    HttpClientModule,
    HttpModule,
    FileUploadModule
  ],

I am getting the below error: 我收到以下错误:

compiler.js:24668 Uncaught Error: 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. ("

    [ERROR ->]<mat-toolbar color="primary">

      <span>Responsive Navigation</span>
"): ng:///ComponentsModule/MatMenuComponent.html@2:4
'mat-sidenav' is not a known element:
1. If 'mat-sidenav' is an Angular component, then verify that it is part of this module.
2. If 'mat-sidenav' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
    <mat-sidenav-container fxFlexFill class="example-container">

      [ERROR ->]<mat-sidenav #sidenav fxLayout="column">

        <div fxLayout="column">
"): ng:///ComponentsModule/MatMenuComponent.html@36:6
'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. ("
    </mat-toolbar>

    [ERROR ->]<mat-sidenav-container fxFlexFill class="example-container">

      <mat-sidenav #sidenav fxLayout="): ng:///ComponentsModule/MatMenuComponent.html@34:4
    at syntaxError (compiler.js:485) [<root>]
    at TemplateParser.parse (compiler.js:24668) [<root>]
    at JitCompiler._parseTemplate (compiler.js:34621) [<root>]
    at JitCompiler._compileTemplate (compiler.js:34596) [<root>]
    at eval (compiler.js:34497) [<root>]
    at Set.forEach (<anonymous>) [<root>]
    at JitCompiler._compileComponents (compiler.js:34497) [<root>]
    at eval (compiler.js:34367) [<root>]
    at Object.then (compiler.js:474) [<root>]
    at JitCompiler._compileModuleAndComponents (compiler.js:34366) [<root>]
    at JitCompiler.compileModuleAsync (compiler.js:34260) [<root>]
    at CompilerImpl.compileModuleAsync (platform-browser-dynamic.js:239) [<root>]
    at PlatformRef.bootstrapModule (core.js:5567) [<root>]
    at eval (main.ts:12) [<root>]

You should import the modules in your component related module.ts file. 您应该将模块导入与组件相关的module.ts文件中。 for example, mat-menu.module.ts file in your case. 例如,您的情况下为mat-menu.module.ts文件。

The problem with what you are doing is, the exported material modules from AppModule are not being accessible inside your custom MatMenuModule 您正在做的问题是,在自定义MatMenuModule内部无法访问AppModule导出的材料模块

Instead create a SharedModule and then import all your @angular/material modules inside it. 而是创建一个SharedModule ,然后将所有@angular/material模块导入其中。 Export it in the same way as you are doing now from SharedModule . SharedModule中以与现在相同的方式导出它。

And then import SharedModule inside your custom MatMenuModule 然后导入SharedModule您的自定义里面MatMenuModule

Check this simple DEMO 检查这个简单的演示

暂无
暂无

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

相关问题 垫子工具栏的宽度与垫子侧面导航不匹配 - mat-toolbar not match width with mat-sidenav 如何将 mat-sidenav 放在 mat-toolbar 下 - How to place mat-sidenav under the mat-toolbar 如何固定 mat-toolbar 和 mat-sidenav 并且在 mat-sidenav-content 内只有一个滚动条? - How to pin mat-toolbar and mat-sidenav and only have a scrollbar inside mat-sidenav-content? 模板解析错误:“mat-sidenav-container”不是已知元素 - Template Parse Error: 'mat-sidenav-container' is not a known element Angular2 + Material:尽管 mat-elevation-z*,mat-toolbar 不会在 mat-sidenav-container 上投下阴影 - Angular2 + Material: mat-toolbar casts no shadow over mat-sidenav-container despite mat-elevation-z* 从 mat-sidenav-container 外部的按钮打开 mat-sidenav - Opening mat-sidenav from a button outside of mat-sidenav-container 当mat-sidenav容器和mat-sidenav位于不同组件中时的问题 - Issue when mat-sidenav-container and mat-sidenav in different components 如何将“mat-toolbar”放在“mat-sidenav”Angular Material 5 的顶部 - How to place "mat-toolbar" on top of "mat-sidenav" Angular Material 5 如何让 mat-sidenav 占据 mat-toolbar 下方的整个垂直空间? - How can I make mat-sidenav take up entire vertical space underneath mat-toolbar? 强制 angular mat-toolbar 和 mat-sidenav 准确占用 100% 的可用垂直空间 - Force angular mat-toolbar and mat-sidenav to take up exactly 100% of the available vertical space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM