Remove the hash fragment from url permanently

∥☆過路亽.° 提交于 2020-04-12 18:18:12

问题


I use oauth authentication to login user. After the user has logged in, get taken from URL. URL like this: http://xxx/callback#access_token=xxx. then redirect to other page. I use this.router.navigateByUrl to redirect, the hash tag is removed but when I click other link,the hash fragment will show again. How can I remove the hash token fragment permanently?

Example:

after use login in //xxx/callback#access_token=xxx

redirect to //xxx/home

then click linkA, the url is //xxx/linkA#access_token=xxx

expected url is //xxx/linkA


回答1:


Looks like this has been fixed in the Angular 2 final release (testing it with 2.1.0 right now).

The following works for me:

router.navigate([]);

Redirects to the "index" page without the hash and it doesn't reappear on subsequent navigation.




回答2:


You should put useHash equal to false at the imports of your app.module

@NgModule({
  bootstrap: [ App ],
  declarations: [App],
  imports: [
    ....
    RouterModule.forRoot(ROUTES, { useHash: false })
  ],
  providers: []
})

putting it to false will stop it from using HashLocationStrategy which is the default in RouterModule.forRoot function




回答3:


I have same issue, my solution is after retrieved access_token, you can set: window.location.hash = ''

It works for me




回答4:


I don't know how you redirect but I assume it's caused by the method you use.

One way to reset the fragment is

<a routerLink="path" fragment="">

(not tested myself yet).

From code

let tree = this.router.navigate(['path'], {relativeTo: this.route, queryParams: ''});

See also https://angular.io/api/router/Router#createurltree




回答5:


If you are for example using auth0 in their doc they do :

this.auth0.parseHash({ _idTokenVerification: false }, (err, authResult) => {
    if (authResult && authResult.accessToken && authResult.idToken) {
      window.location.hash = "";
    } 
});

Instead do:

this.auth0.parseHash({ _idTokenVerification: false }, (err, authResult) => {
    if (authResult && authResult.accessToken && authResult.idToken) {
      this.router.navigate["/home"]
    } 
});

This will remove the whole hash



来源:https://stackoverflow.com/questions/38779243/remove-the-hash-fragment-from-url-permanently

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