Angular 9中无法识别共享模块中的Angular组件

时间:2020-03-05 15:43:10

标签: angular

我有一个共享的模块,可以导出材料成分,反应形式和其他内容,但是当我尝试这样做时,会出现以下错误:

如果“ mat-menu”是Angular组件,则请验证它是否属于Angular组件 此模块。

直到现在我一直在尝试,但没有解决。我该怎么解决?

下面,我将展示我的所有模块,以便您了解我正在尝试做的事情。我的pages.moduleauth.module是那些试图使用shared.module的组件。

应用模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
//My imports
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { RouterModule } from '@angular/router';
//import { HttpClientModule} from '@angular/common/http';
import { CoreModule } from './../app/core/core.module';
@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgbModule,
    BrowserAnimationsModule,
    RouterModule,
    CoreModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

材料模块

import { NgModule } from '@angular/core';
import {MatMenuModule} from '@angular/material/menu';
import {MatIconModule} from '@angular/material/icon';
import {MatToolbarModule} from '@angular/material/toolbar';
import {MatButtonModule} from '@angular/material/button';
import {MatCardModule} from '@angular/material/card';
import {MatInputModule} from '@angular/material/input';
import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatRadioModule} from '@angular/material/radio';
import { FormsModule } from '@angular/forms';
import {MatGridListModule} from '@angular/material/grid-list';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {MatSnackBarModule} from '@angular/material/snack-bar';
const MaterialComponents = [
    MatMenuModule,
    MatIconModule,
    MatToolbarModule,
    MatButtonModule,
    MatCardModule,
    MatInputModule,
    MatCheckboxModule,
    MatRadioModule,
    FormsModule,
    MatGridListModule,
    MatProgressSpinnerModule,
    MatSnackBarModule,

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

共享模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MaterialModule } from 'src/app/shared/material/material.module';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    ],
  imports: [
    CommonModule,
    MaterialModule,
    ReactiveFormsModule,

  ],
  exports: [
    MaterialModule,
    ReactiveFormsModule,

  ]
})
export class SharedModule { }

身份验证模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
//import { AuthRoutingModule } from './auth-routing.module';
import { HomeComponent } from 'src/app/auth/home/home.component';
import { HomeAdminComponent } from 'src/app/auth/home-admin/home-admin.component';
//import{ RoutingModule } from 'src/app/modules/routing.module';

@NgModule({
  declarations: [HomeComponent,HomeAdminComponent],
  imports: [
    CommonModule,
    SharedModule,
  ],
})
export class AuthModule { }

页面模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
import { AdminDashboardComponent } from 'src/app/pages/admin-dashboard/admin-dashboard.component';
import { UsersDashboardComponent } from 'src/app/pages/users-dashboard/users-dashboard.component';
import { RoutingAdminModule } from 'src/app/pages/routing-admin.module';
import { RoutingUsersModule } from 'src/app/pages/routing-users.module';

@NgModule({
  declarations: [AdminDashboardComponent, UsersDashboardComponent],
  imports: [
    CommonModule,
    SharedModule,
    RoutingAdminModule,
    RoutingUsersModule,
  ]
})
export class PagesModule { }

3 个答案:

答案 0 :(得分:0)

问题是您不小心将数组与Imports数组嵌套在一起。解决它的方法是

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

在代码块中,您错误地将imports数组的第一个元素设置为称为“ MaterialComponents”的数组; 您不小心在数组中创建了一个数组。

您的代码和解决方案之间的区别是扩展语法(三个句点...)

在这种情况下,spread语法实际上只是将“ MaterialComponents”的元素追加到您想要的导入数组中。

答案 1 :(得分:-1)

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

或者只是:

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

答案 2 :(得分:-1)

这就是我的解决方法

将已将共享模块导入到应用程序模块中的模块

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
//My imports
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
//import { HttpClientModule} from '@angular/common/http';
import { CoreModule } from './../app/core/core.module';
import { PagesModule } from './pages/pages.module';
import { AuthModule } from './auth/auth.module';
import { PagesUsersModule } from 'src/app/pages-users/pages-users.module';

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    CoreModule,
    AuthModule,

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

将共享模块导入身份验证模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
import { AuthRoutingModule } from './auth-routing.module';
import { HomeComponent } from 'src/app/auth/home/home.component';
import { HomeAdminComponent } from 'src/app/auth/home-admin/home-admin.component';

@NgModule({
  declarations: [HomeComponent,HomeAdminComponent],
  imports: [
    AuthRoutingModule,
    CommonModule,
    SharedModule,
  ],

})
export class AuthModule { }

将共享导入“页面”模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SharedModule } from '../shared/shared.module';
import { AdminDashboardComponent } from 'src/app/pages/admin-dashboard/admin-dashboard.component';
import { RoutingAdminModule } from 'src/app/pages/routing-admin.module';

@NgModule({
  declarations: [AdminDashboardComponent],
  imports: [
    CommonModule,
    SharedModule,
    RoutingAdminModule,
  ],

})
export class PagesModule { }

应用程序路由模块

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

//My imports
import { HomeComponent } from './auth/home/home.component';
import { AuthGuard } from 'src/app/shared/guards/auth.guard';
import { HomeGuard } from 'src/app/shared/guards/home.guard';
import { AdminGuard } from 'src/app/shared/guards/admin.guard';

const routes: Routes = [

  { path: 'admin', loadChildren: './pages/pages.module#PagesModule', canActivate: [AdminGuard] },
  { path: 'users', loadChildren: './pages-users/pages-users.module#PagesUsersModule', canActivate: [AuthGuard] },
  { path: '', loadChildren: './auth/auth.module#AuthModule', canActivate: [HomeGuard] },
  { path: '**', component: HomeComponent, canActivate:[HomeGuard] }

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

共享模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MaterialModule } from 'src/app/shared/material/material.module';
import { ReactiveFormsModule } from '@angular/forms';

import { RouterModule } from '@angular/router';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    MaterialModule,
    ReactiveFormsModule,
    RouterModule,
    NgbModule
  ],
  exports: [
    MaterialModule,
    ReactiveFormsModule,
    RouterModule,
    NgbModule
  ]
})
export class SharedModule { }