Angular2 Router:将井号标签添加到url [英] Angular2 Router: add hashtag to url

查看:227
本文介绍了Angular2 Router:将井号标签添加到url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular2 Seed应用程序,您可以在官方仓库中找到它.如您所见,这里我们导入了angular2/router,我们正在使用它来创建应用程序的基本路由.

I'm using Angular2 Seed application, and you can find it in the official repo. As you can see, here we have angular2/router imported, and we're using it to create the basic routing of the application.

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一样)

My question is: how can I configure router to add hashtag in my url, to make it looks like: localhost:5555/#/about. Is there any beautiful and easy way to make it? (as earlier with $locationProvider)

我知道这很奇怪,但是我曾经喜欢url中的这个标签,而我的apache-config也曾经喜欢它.当然,我可以非常容易且正确地更改我的httpd.conf文件,但是我真的想弄清楚如何使用Angular2 Router简单地添加井号.

I know it's bizarre, but I used to love this hashtag in url, and my apache-config also used to love it. Of course, I can change my httpd.conf file, very easy and properly, but I really want to figure out, how to simply add hashtag, with Angular2 Router.

推荐答案

在应用程序启动文件中,您需要在调用引导程序时提供位置策略,

In your application startup file, you need to provide locationstrategy while calling bootstrap,

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

class MyDemoApp {
    //your code
}

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

这篇关于Angular2 Router:将井号标签添加到url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆