2016-09-07 10 views
1

this.router.navigateは使用できません。オリジナル例外:ルーター用のプロバイダはありません!角度2 RC5

これは私です: app.module.ts

import {NgModule, NgModuleMetadataType}  from '@angular/core'; 
    import { BrowserModule } from '@angular/platform-browser'; 
    import {FormsModule} from '@angular/forms'; 
    import { HttpModule } from '@angular/http'; 
    ... 
    import {routing} from "./app.routing"; 
    import {entry} from "./entry.component"; 
imports: [ 
     BrowserModule, 
     FormsModule, 
     routing, 
     HttpModule, 
    ], 

テストコンポーネント

import { Component } from '@angular/core'; 
import {HttpClient} from "./HttpClient.component"; 
import {Router} from "@angular/router-deprecated"; 

@Component({ 
    templateUrl: 'templates/entry.html' 
}) 
export class entry { 
    ... 
    constructor(head:HeaderComponent, private httpClient: HttpClient, private router: Router) { 
     this.httpClient = httpClient; 
    } 
    nav_test(){ 
     this.router.navigate(['search']); 
    } 
} 

import { Routes, RouterModule } from '@angular/router'; 
const appRoutes: Routes = [ 
    { 
     path: '', 
     redirectTo: '/home', 
     pathMatch: 'full', 
    }, 
    { 
     path: 'home', 
     component: HomeComponent 
    }, 
    { 
     path: 'search', 
     component: SearchComponent 

    } 

]; 
export const routing = RouterModule.forRoot(appRoutes, {useHash: true}); 

をapp.routingし、最後に私はこのエラーがあります:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./entry class entry_Host - inline template:0:0 ORIGINAL EXCEPTION: No provider for Router!

ありがとうございます!

答えて

1

問題は、あなたが、

import { Router } from '@angular/router'; 
を使用する必要があります

import {Router} from "@angular/router-deprecated"; 

を使用している、あなたのテストコンポーネントのインポートと

です

これが役立つことを願っています!

+0

あなたのためにLOLのthnaks答え、私は分遅いxD – Vendicto

+0

どういうわけか角度のチームはまだルータを非推奨にしている、それはいくつかのアラームを開始する必要があります、あなたがそれを使用しているマシンは火についている必要があります.. –

0

useHashかわからない:真の作品が、私は@NgModuleに拠点展開を指定するには:

providers:[{provide: LocationStrategy, useClass: HashLocationStrategy}]

そして私は、最新のルータではなく、非推奨のいずれかを使用します。

作業RC.6コードサンプルはここにある:https://github.com/Farata/angular2typescript/tree/master/chapter3/router_samples

関連する問題