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

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

问题描述

我正在尝试从 Angular 2 的 url 中删除 # 符号,但我找不到关于如何在不产生任何问题的情况下删除它的任何好的解释.

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.

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

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

另外,如果您能告诉我这是否是一个好的做法(删除 #)或是否会影响应用程序的 SEO(或改进它),我将不胜感激.

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:我使用带有打字稿的 Angular 2

PS: I am using Angular 2 with typescript

推荐答案

正如@Volodymyr Bilyachat 所指出的,PathLocationStrategy 是默认的 定位策略 在 Angular2 中,如果 # 出现在 url 中,一定是在某处被覆盖了.

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.

除了模块提供者,检查你的模块导入,它也可以通过提供 { useHash: true } 作为 RouterModule.forRoot 的第二个参数来覆盖:

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
]

另请注意,在使用 PathLocationStrategy 时,您需要配置您的网络服务器,以便为所有请求的位置提供 index.html(应用程序的入口点).

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.

以下是一些流行的 Web 服务器的配置示例:https://angular.io/guide/deployment#fallback-configuration-examples

Here are configuration examples for some of the popular web servers: https://angular.io/guide/deployment#fallback-configuration-examples

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

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