Uncaught Error: Unexpected module 'RouterModule' declared by the module 'AppRoutingModule'. Please add a @Pipe/@Directive/@Component annotation

时间:2018-08-22 13:46:30

标签: angular angular-ui-router router

Hello fellow programmer friends,

Need help for the error in the title.

This is my routing module code

app-routing.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

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

import { routes } from './routes';

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

I tried removing RouterModule from the declarations array as well but not getting rectified.

Please help.

3 个答案:

答案 0 :(得分:2)

在声明中,您不能添加任何模块。如错误所示:在声明数组中仅允许声明Components,Pipes或Directives

答案 1 :(得分:1)

Understood what mistake I did.

I placed the RouterModule in declarations instead of exports.

This thread may assist future learners who make the same mistake like me.

答案 2 :(得分:1)

RouterTestingModule添加到导入数组。

imports: [HttpClientTestingModule, RouterTestingModule]