Angular 2从URL中删除哈希(#) [英] Angular 2 Remove Hash (#) from the URL

查看:101
本文介绍了Angular 2从URL中删除哈希(#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Angular 2的url中删除#号,但是我找不到任何关于如何删除它的好解释,而不会产生任何问题。



我记得在AngularJS 1中添加 $ locationProvider.html5Mode(true);



更容易如果你能告诉我这是否是一种好的做法(删除#)或可能影响应用程序的搜索引擎优化(或改进它)。



PS:我正在使用Angular 2 with typecript

解决方案

正如@Volodymyr Bilyachat指出的, PathLocationStrategy 是Angular2中的默认位置策略,如果存在于url中,它肯定会在某处被覆盖。

在模块提供程序旁边,检查您的模块导入,也可以通过提供 {useHash:true} 作为 RouterModule.forRoot 的第二个参数:

  imports: [
...
RouterModule.forRoot(routes,{useHash:true})//移除第二个参数
]

还要注意,当使用 PathLocationStrategy 时,你需要配置你的web服务器来服务 index.html所有请求的位置(应用程序的入口点)。


I am trying to remove the # sign from the url in Angular 2 but I couldn't find any good explanation about how to remove it without generate any problem.

I remember on AngularJS 1 it was easier adding $locationProvider.html5Mode(true);

Also I would appreciate if you can tell me if this is a good practice (removing #) or could affect the SEO for the application (or improve it).

PS: I am using Angular 2 with typescript

解决方案

As @Volodymyr Bilyachat pointed out, PathLocationStrategy is a default location strategy in Angular2, and if the # is present in the url, it must have been that's overridden somewhere.

Beside the module providers, check your module imports, it can also be overridden by providing the { useHash: true } as the second argument of the RouterModule.forRoot:

imports: [
    ...
    RouterModule.forRoot(routes, { useHash: true })  // remove second argument
]

Also note that when using PathLocationStrategy you need to configure your web server to serve index.html (app's entry point) for all requested locations.

这篇关于Angular 2从URL中删除哈希(#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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