Angular2路由器:3.0.0-alpha.8 +无法解析provideRouter的所有参数:(?,?)

时间:2016-07-05 06:40:08

标签: angular angular2-routing

使用路由器版本“3.0.0-alpha.8”的angular2项目。加载apis时显示错误:

Can't resolve all parameters for provideRouter: (?, ?) .

Error : BaseException$1@http://localhost:3000/node_modules/@angular/compiler//bundles/compiler.umd.js:955:27

以下是使用的文件:

app.routes.ts

import { provideRouter, RouterConfig } from '@angular/router';
import { HeroDetailComponent } from './First/first.component';
import { HeroListComponent } from './First/first-list.component';
import { AppComponent } from './app.component';
import { LoginComponent } from './Login/login.component';

export const routes: RouterConfig = [

{ path: 'heroes', component: HeroListComponent },
{ path: 'first', component: HeroDetailComponent },
{ path: 'login', component: LoginComponent }

];

export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];

app.component.ts

 import { Component } from '@angular/core';
 import { Hero } from './Hero/hero.component';
 import { HeroDetailComponent } from './First/first.component';
 import { ROUTER_DIRECTIVES, Router, provideRouter } from    '@angular/router';
 import { LoginComponent } from './Login/login.component';
 import { Http, Response } from '@angular/http';
 import { Observable } from 'rxjs/Rx';


@Component({
   selector: 'my-app',
   templateUrl: 'app/app.component.html',
   directives: [ HeroDetailComponent, ROUTER_DIRECTIVES ],
   providers: [provideRouter]
})
export class AppComponent {
   constructor(private router:Router) {
   }
   title = 'Tour of Heroes';
   selectedHero: Hero;
   onSelect(hero: Hero) { this.selectedHero = hero; }
   }

请帮忙。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您没有正确提供路由器。 转到你的main.ts文件,在那里你引导角度并找到你拥有的提供者列表。它看起来应该类似于:

bootstrap(AppComponent, 
    [
        // Providers
    ]

您需要将const APP_ROUTER_PROVIDERS添加到此列表中,如下所示:

import {APP_ROUTE_PROVIDERS} from 'app.routes';
bootstrap(AppComponent, 
    [
        APP_ROUTER_PROVIDERS
    ]

然后从providers: [provideRouter]

中删除app.component.ts