2016-08-08 50 views
0

我想爲我的angular2應用程序做一個路由。我看到很多問題,如thisthis但他們都沒有幫助我。沒有RouterOutletMap的供應商!在angular2應用程序

這裏是我的app.route.ts

import { provideRouter, RouterConfig } from '@angular/router'; 
import {FormComponent} from './form.component'; 
import {AboutComponent} from './about-this.component'; 

const routes: RouterConfig = [ 
    { path:'home' , component: FormComponent }, 
    { path:'about', component: AboutComponent }, 
    { path:'**' , component: FormComponent } 
]; 

export const appRouterProviders = [ 
    provideRouter(routes) 
] 

,然後這裏是我的根組件:

import { ROUTER_DIRECTIVES, Router } from '@angular/router'; 
import { Component } from '@angular/core'; 
import {NavbarComponent} from './shared/navbar.component'; 
import {appRouterProviders} from './app.routes' 


@Component({ 
    moduleId: module.id, 
    selector: 'app-root', 
    templateUrl: 'app.component.html', 
    styleUrls: ['app.component.css'], 
    directives:[NavbarComponent,ROUTER_DIRECTIVES], 
    providers: [appRouterProviders] 
}) 

export class AppComponent { 
    title = "Here is Root!"; 
    } 

,並在寺廟(app.component.html):

<router-outlet></router-outlet> 

,你可以請參閱我已使用./app.routes作爲我的路線提供者。 因此,我期望,當我使用http://localhost:4200/home它帶來了表單組件,但它不會發生。另外,控制檯中沒有錯誤。 什麼問題?

更新: 我也bootstraped我的應用程序main.ts

import { bootstrap } from '@angular/platform-browser-dynamic'; 
import {AppComponent} from './app.component'; 
import {appRouterProviders } from './app.routes'; 

bootstrap(AppComponent,[appRouterProviders]); 
+0

當您的頁面加載時,刷新它並查看是否發生錯誤。 – Bean0341

回答

0

路由器需要基於關閉在上面的答案我有一個評論的bootstrap

bootstrap(AppComponent, [appRouterProviders]) 
+0

你在'bootstrap'中只提供了**嗎?還是**在bootstrap中提供了**?它應該是**只**。 –

+0

你一定要在'bootstrap()'中提供它。如果這不起作用,它與你提供'appRouterProviders'的地方無關。 –

+0

你可以在Plunker中重現嗎? –

0

要提供你可以嘗試幾個解決方案...

首先:在package.json文件中更改

"@angular/router": "whatever version you have" 

這個

"@angular/router": "3.0.0-beta.2" 

此外,如果你的package.json文件看起來並不像快速入門here那麼我會建議在應對它給你的項目,讓我上面提到的變化,在你的項目上運行一個npm安裝。然後嘗試啓動你的應用程序,看看你得到什麼。

相關問題