2016-03-02 240 views
4

我試圖設置一個簡單的路線,同時學習Angular 2,每當我點擊一個鏈接,瀏覽器路由到新的路由,但瀏覽器正在請求所有的資源(不表現爲單頁面應用程序)。Angular 2路由器刷新頁面,同時路由

我的索引文件,

<!--... . . various scripts and styles . . . . . ---> 
<script src="node_modules/angular2/bundles/angular2.dev.js"></script> 
<script src="node_modules/angular2/bundles/router.dev.js"></script> 
<script src="node_modules/angular2/bundles/http.dev.js"></script> 

<script> 
    System.config({ 
    packages: {   
     app: { 
     format: 'register', 
     defaultExtension: 'js' 
     } 
    } 
    }); 
    System.import('app/main') 
     .then(null, console.error.bind(console)); 
</script> 
</head> 
<body> 
    <app></app> 
</body> 

應用程序的來源,

main.ts

import {bootstrap} from 'angular2/platform/browser'; 
import {RoutingApp} from './routing/routing.app' 
import {ROUTER_PROVIDERS} from 'angular2/router' 

bootstrap(RoutingApp, [ROUTER_PROVIDERS]); 

RoutingApp

import {Component} from "angular2/core" 
import {RouteComponent} from './route.component' 
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router'; 


@Component({ 
    selector : 'app', 
    template : ` 
     go to this <a href="/link">link</a> 
     <br /> 
     <router-outlet></router-outlet> 

    `, 
    directives: [ROUTER_DIRECTIVES], 
    providers: [ROUTER_PROVIDERS] 
}) 
@RouteConfig([ 
    {path: '/link', name: 'Link', component: RouteComponent} 
]) 
export class RoutingApp{ 

} 

RouteComponent

import {Component} from 'angular2/core' 

@Component({ 
    template: ` 
     hello from RouteComponent 
    ` 
}) 
export class RouteComponent{} 

我做錯了什麼?有角版本是2.0.0-beta.7

感謝您的任何見解。

回答

7

您應該使用routerLink指令導航到路線:

<a [routerLink]="['Link']">link</a> 

這個指令是直接可用的,因爲你指定ROUTER_DIRECTIVES到組件的directives屬性