Angular 2在Plunker中没有RouterOutletMap的提供者

时间:2016-08-17 12:44:42

标签: angular angular2-routing plunker

我需要帮助在Plunker中设置Angular 2路由。 继续为RouterOutletMap提供无提供程序错误。

Plunker:https://plnkr.co/edit/ZfsaKj0r4mGztxbQ43UZ?p=preview

main.ts

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
import { appRouterProviders }   from './app.routes';
platformBrowserDynamic().bootstrapModule(AppModule, [appRouterProviders])

app.ts

import {Component, NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { ROUTER_DIRECTIVES } from '@angular/router';
//import { ROUTER_PROVIDERS, Routes, ROUTER_DIRECTIVES, Router } from '@angular/router';

@Component({
  selector: 'my-app',
  template: `
    <div>
       <router-outlet></router-outlet>
    </div>
  `,
  directives: [ROUTER_DIRECTIVES],
  //providers: [ROUTER_PROVIDERS]
})
export class App {
  constructor() {}
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

app.routes.ts

import { provideRouter, RouterConfig }  from '@angular/router';
import { TopicsComponent } from './topics.component';
import { PageNotFoundComponent } from './page-not-found.component';

const routes: RouterConfig = [

  {
    path: '',
    component: TopicsComponent
  },
  {
    path: '**',
    component: PageNotFoundComponent
  },
];

export const appRouterProviders = [
  provideRouter(routes)
];

topics.component.ts

import { Component} from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
//import { ROUTER_PROVIDERS, Routes, ROUTER_DIRECTIVES, Router } from '@angular/router';

@Component({
  selector: 'topics',
  templateUrl: `
    <h1>{{title}}</h1>
    <div *ngFor="let topic of topics">
      <a [routerLink]="'/' + topic.title" routerLinkActive="active">{{ topic.title }}</a>
    </div>
  `,
  directives: [ROUTER_DIRECTIVES],
  //providers: [ROUTER_PROVIDERS]
})

export class TopicsComponent { 
    title = 'Welcome to the topics component!';
    topics: [
        {title: 'angular2'}, 
        {title: 'react'}, 
        {title: 'angular1'}
    ];

}

2 个答案:

答案 0 :(得分:0)

你是混合RC4和RC5 ..小心!

请查看此处的文档:https://angular.io/docs/ts/latest/guide/router.html

Theres也是一名工作人员:http://plnkr.co/edit/nRoudQv5Y52wH8OBRALO?p=preview

// there's too much code to post here! :)
// this is just to fake some code.. ignore it! ;)

答案 1 :(得分:0)

我假设您正在使用RC5,如果确实如此,您应该使用新的 RouterModule

参考文献:https://angular.io/docs/ts/latest/tutorial/toh-pt5.html