角材料:“在'./material/material.module'中找不到导出'MaterialComponents'

时间:2020-03-20 19:17:36

标签: javascript angular typescript angular-material

我是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 { }

2 个答案:

答案 0 :(得分:2)

将其更正为MaterialModule

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

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

此外,请注意,如果您想创建一个公共的共享模块来合并所有其他import,则不需要exportmodules相同的东西:

@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的语法错误

答案 1 :(得分:0)

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

const MaterialComponents = [
  MatButtonModule
];

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

这等于一个数组数组:

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

不是这样吗?

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