Angular2路由器:将URL标签添加到url

前端之家收集整理的这篇文章主要介绍了Angular2路由器:将URL标签添加到url前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Angular2 Seed应用程序,您可以在 official repo中找到它.正如您所看到的,这里我们已经导入了angular2 / router,我们正在使用它来创建应用程序的基本路由.

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 {}

我的问题是:如何配置路由器在我的网址中添加主题标签,使其看起来像:localhost:5555 /#/ about.有没有美丽而简单的方法来制作它? (与之前的$locationProvider一样)

我知道这很奇怪,但我曾经在网址中喜欢这个标签,而我的apache-config也曾经喜欢它.当然,我可以更改我的httpd.conf文件,非常简单和正确,但我真的想弄清楚,如何简单地添加hashtag,与Angular2路由器.

解决方法

在您的应用程序启动文件中,您需要在调用bootstrap时提供locationstrategy,

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

class MyDemoApp {
    //your code
}

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

猜你在找的Angularjs相关文章