2016-08-16 11 views
1

Angular2アプリケーションに経路を追加しようとしています(私はAngular2 rc4を使用しています)。しかし、私は新しいページに読み込み中に問題が発生しています。私のapp.comonentrouterLinkですべての経路をうまく読み込めますが、私がURL経由で経路にナビゲートすると、私には経路が得られません/経路。また、エラーのないhome.componentもロードされます。私はHTTP_PROVIDERSを追加しようとしましたが、そこに運がありません。この問題の原因は何ですか?Angular2 - Can not GET/

boot.ts

import {bootstrap} from '@angular/platform-browser-dynamic' 
import {HTTP_PROVIDERS} from '@angular/http'; 
import {provideRouter} from '@angular/router'; 
// Componets 
import {AppComponent} from './app/app.component' 
import {APP_ROUTER_PROVIDERS} from './app/app.routes'; 

bootstrap(AppComponent, [ 
    provideRouter(APP_ROUTER_PROVIDERS), 
    HTTP_PROVIDERS, 
    APP_ROUTER_PROVIDERS 
]).catch(err => console.error(err)); 

app.routes.ts

import {provideRouter, RouterConfig} from '@angular/router'; 
// Components 
import {HomeComponent} from './home/home.component'; 
import {StoreComponent} from './store/store.component'; 


export const routes: RouterConfig = [ 

    { path: '', component: HomeComponent }, 
    { path: 'store', component: StoreComponent }, 

]; 

export const APP_ROUTER_PROVIDERS = [ 
    provideRouter(routes) 
]; 

home.component.ts

import {Component} from '@angular/core'; 

//const template = require('./home.component.html'); 
const template = 'src/app/home/home.component.html'; 

@Component({ 
    selector: 'my-home', 
    templateUrl: template 
}) 

export class HomeComponent { 

} 

store.component.ts

import {Component} from '@angular/core'; 

const template = 'src/app/store/store.component.html'; 
const styles = '/store.component.css'; 

// Mock Models 
interface Product { 
    id: number; 
    logo: string; 
    name: string; 
    price: number; 
}; 
let Categories:string[] = ['Convertible','Sports Car','Truck','Suv'] ; 
let Products:Product[] = [ 
    { id: 0, logo: '/images/Logo.png', name: "Food ", price: 1000 }, 
    { id: 1, logo: '/images/Logo.png', name: "Shelter", price:2000 }, 
    { id: 2, logo: '/images/Logo.png', name: "Health", price:3000 }, 
    { id: 3, logo: '/images/Logo.png', name: "Advocacy", price: 4000}, 
    { id: 4, logo: '/images/Logo.png', name: "Funding", price: 5000 }, 
]; 

@Component({ 
    selector: 'my-store', 
    templateUrl : template 
}) 

export class StoreComponent { 
    categories:string[] = Categories ; 
    products:Product[] = Products ; 

} 

答えて

3

bootstrap(...)から

APP_ROUTER_PROVIDERS 

この行を削除します。これは、すでに私はpathMatch: 'full'がこのルート

{ path: '', component: HomeComponent, pathMatch: 'full' }, 

のために必要であると思いますが、また、他の人のためにせずに動作するようですprovideRouter(...)

により提供されます。

+0

src/app/app.component.htmlでエラーが発生しました:0:0vendor.bundle.js:44226:14 ORIGINAL EXCEPTION:エラー:経路 'undefined'の設定が正しくありません:コンポーネント、redirectTo、子供は – John

+0

pathMatch: 'full'を提供する必要があります、またそれを修正しません。 – John

+0

'HomeComponent'や' StoreComponent'の問題や、これらのインポートのように聞こえます。 –

関連する問題