我在向angular添加简单规则时遇到问题。我正在关注指南(https://codecraft.tv/courses/angular/routing/route-configuration/),我的代码如下:
app.module.ts
import {Routes, RouterModule} from "@angular/router";
const routes: Routes = [
{ path: '', component: HomeComponent },
];
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
ContentComponent,
FooterComponent,
CuriositiesComponent,
HomeComponent,
RouterModule.forRoot(routes, {useHash: true}),
],
imports: [
BrowserModule,
SwiperModule
],
providers: [],
bootstrap: [AppComponent]
})
不幸的是,在编译过程中出现了一些错误,我不知道问题出在哪里:
ERROR in src/app/app.module.ts(20,11): error TS2345: Argument of type '{ declarations: (ModuleWithProviders | typeof HomeComponent | typeof AppComponent)[]; imports: (t...' is not assignable to parameter of type 'NgModule'.
Types of property 'declarations' are incompatible.
Type '(ModuleWithProviders | typeof HomeComponent | typeof AppComponent)[]' is not assignable to type '(any[] | Type<any>)[]'.
Type 'ModuleWithProviders | typeof HomeComponent | typeof AppComponent' is not assignable to type 'any[] | Type<any>'.
Type 'ModuleWithProviders' is not assignable to type 'any[] | Type<any>'.
Type 'ModuleWithProviders' is not assignable to type 'Type<any>'.
Property 'apply' is missing in type 'ModuleWithProviders'.
答案 0 :(得分:2)
declarations
装饰器的 NgModule
元数据选项仅包含Component
,Pipe
&amp; Directive
。您无法将RouterModule
放在declarations
选项中。你应该把imports
选项放在里面
imports: [
//Removed RouterModule from declarations and shifted inside import.
RouterModule.forRoot(routes, {useHash: true}),
BrowserModule,
SwiperModule
],