Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the hash fragment from url permanently

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

like image 607
Wenbo Chu Avatar asked Aug 05 '16 00:08

Wenbo Chu


Video Answer


2 Answers

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.

like image 124
netmikey Avatar answered Oct 06 '22 15:10

netmikey


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

like image 38
Mahin Khan Avatar answered Oct 06 '22 15:10

Mahin Khan