2015-10-20 62 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