Hash Location Strategy in Angular 2

让人想犯罪 __ 提交于 2019-11-28 01:14:00
Günter Zöchbauer

ROUTER_PROVIDERS should not be added to child components,

only to

providers: [ROUTER_PROVIDERS]

or alternatively only to

bootstrap(AppComponent, [ROUTER_PROVIDERS]);

HTTP_PROVIDERS are in my opinion also a better fit for root component or bootstrap() but it doesn't break anything to add them somewhere else.

(See also Routing error with angular 2 and IIS)

You can use the option "useHash" in RouterModule.forRoot().

RouterModule.forRoot(appRoutes, {useHash: true});

https://discuss.atom.io/t/angular-2-routes-breaking-on-electron-app-refresh/28370/4

Everything worked fine with the sample code OP posted as with what is in the accepted answer. But as a minor note, the format required to changing the Hash Location Strategy in the bootstrap file as of RC.4 goes like this:

{ provide: LocationStrategy, useClass: HashLocationStrategy },

It is recommended to use the HTML 5 style (PathLocationStrategy) as location strategy in Angular

Because

  1. It produces the clean and SEO Friendly URLs that are easier for users to understand and remember.
  2. You can take advantage of the server-side rendering, which will make our application load faster, by rendering the pages in the server first before delivering it the client.

Use Hashlocationstrtegy only if you have to support the older browsers.

Click Here for More info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!