2015-10-20 103 views
3

我使用Angular2種應用程序,你可以在official repo找到它。正如你所看到的,在這裏我們導入了angular2/router,我們用它來創建應用程序的基本路由。Angular2路由器:#標籤添加到URL

import {Component, ViewEncapsulation} from 'angular2/angular2'; 
import { 
    RouteConfig, 
    ROUTER_DIRECTIVES 
} from 'angular2/router'; 
... 
@RouteConfig([ 
    { path: '/', component: HomeCmp, as: 'Home' }, 
    { path: '/about', component: AboutCmp, as: 'About' } 
]) 
export class AppCmp {} 

我的問題是:如何配置路由器添加#標籤在我的網址,使之看起來像:本地主機:5555 /#/約。有沒有什麼美麗和簡單的方法來做到這一點? (如前面$ locationProvider)

我知道這很離奇,但我以前很喜歡這個主題標籤中的網址,我的Apache的配置也曾經喜歡它。當然,我可以很容易地更改我的httpd.conf文件,但我真的很想弄清楚,如何在Angular2 Router中簡單添加hashtag。

回答

7

在應用程序啓動文件,你需要提供locationstrategy同時呼籲引導,

import {LocationStrategy, HashLocationStrategy} from 'angular2/router'; 

class MyDemoApp { 
    //your code 
} 

bootstrap(MyDemoApp,[provide(LocationStrategy, {useClass: HashLocationStrategy})]); 
+0

它的工作原理!非常感謝。 – punov

+1

看來它現在位於'@ angular/common'下。 https://angular.io/docs/ts/latest/api/common/index/HashLocationStrategy-class.html – Enis