Angular 2中没有路由器提供商

时间:2017-09-08 07:54:49

标签: javascript angular angular-routing angular-providers

作为一名新手,我在Angular 2中设置路线时遇到问题。 虽然我很确定,但这是我缺少的基本内容。

我想要的只是我的App Component加载并向我显示标题。在它下方,一个链接将加载路由器插座部分中的contact-list component

import { Component,OnInit } from '@angular/core';
import { Contact } from './contact';

@Component({
  selector: 'app-root',
  template: `
    <h1>{{title}}</h1>
    <a routerLink="/contact-list">Contact List</a>
    <router-outlet></router-outlet>
    `
})
export class AppComponent{
  title = 'Welcome to my Dream App!';
}

以下是app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { ContactCardComponent } from './contact-card.component';
import { ContactsListComponent } from './contacts-list.component';

RouterModule.forRoot([
  {
    path:'',
    redirectTo:'app',
    pathMatch:'full',
  },
  {
    path:'app',
    component:AppComponent
  },
  {
    path:'contact-list',
    component:ContactsListComponent
  },
  {
    path:'contact-details/:id',
    component:ContactCardComponent
  }
]);

@NgModule({
  declarations: [
    AppComponent,
    ContactsListComponent,
    ContactCardComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

但是说:

error_handler.js:54 EXCEPTION: Error in ./AppComponent class AppComponent - inline template:2:4 caused by: No provider for Router!

没有路由器提供商。

我做错了什么?

更新:

如果我这样做:

  template: `
    <h1>{{title}}</h1>
    <contacts-list></contacts-list>
    `
app.component.ts

,它运作正常。

解决方案发布后,我看到重复的一行:

enter image description here

1 个答案:

答案 0 :(得分:4)

您应该将RouterModule导入模块定义和“导入”数组:

const ROUTES = [
  // HERE ROUTES DEFINITIONS
]

@NgModule({
    imports: [
        RouterModule.forChild(ROUTES)
    ],
    declarations: [],
    providers: []
})
export default class YourModuleDefinition{

}

所以你的代码应该是这样的:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { ContactCardComponent } from './contact-card.component';
import { ContactsListComponent } from './contacts-list.component';

@NgModule({
   declarations: [
     AppComponent,
     ContactsListComponent,
     ContactCardComponent
   ],
   imports: [
     BrowserModule,
     FormsModule,
     HttpModule,
     RouterModule.forRoot([
       {
          path:'',
          redirectTo:'app',
          pathMatch:'full',
       },
       {
          path:'app',
          component:AppComponent
       },
       {
          path:'contact-list',
          component:ContactsListComponent
       },
       {
          path:'contact-details/:id',
          component:ContactCardComponent
        }
     ]);
   ],
   providers: [],
   bootstrap: [AppComponent]
 })
 export class AppModule { }

使用提供的配置并添加到imports数组的RouterModule的forRoot静态类方法提供了模块的路由问题。

此处有更多信息:https://angular.io/guide/ngmodule