5

作为一个新手,我在 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,它工作正常。

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

在此处输入图像描述

4

1 回答 1

4

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

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

RouterModule 的 forRoot 静态类方法具有提供的配置并添加到导入数组中,为模块提供了路由关注点。

更多信息在这里:https ://angular.io/guide/ngmodule

于 2017-09-08T08:01:10.017 回答