首页 > 解决方案 > Angular Material:“在'./material/material.module'中找不到导出'MaterialComponents'

问题描述

我是 Angular 的新手,在导出材质模块时遇到了一些问题。这是错误:

(编译失败。)./src/app/app.module.ts 17:12-30“在'./material/material.module'中找不到导出'MaterialComponents'

这是材质模块:

import { NgModule } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';

const MaterialComponents = [
  MatButtonModule
];

@NgModule({
  exports: [MaterialComponents],
  imports: [MaterialComponents],
})
export class MaterialModule { }

应用模块:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MaterialComponents } from './material/material.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MaterialComponents
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

标签: javascriptangulartypescriptangular-material

解决方案


改正为MaterialModule

import { MaterialModule } from './material/material.module';

  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MaterialModule //< -- HERE
  ],

另外,请注意,import如果export您想创建一个通用共享模块来整合所有其他模块,则modules不需要这样做:

@NgModule({
  exports: [MatButtonModule, OtherMatModules...],
  imports: [],
})
export class MaterialModule { }

虽然,错误来了,因为你没有把export关键字放在前面

export const MaterialComponents = [
  MatButtonModule
];

即使你把出口,你最终会得到错误,所以只需使用我上面的建议。用你传递的array价值观array

在你的情况下,下面

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

相当于:


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

这是嵌套数组,并且 angular 的语法错误


推荐阅读