清除Angular 6中的路由器片段

Har*_*lue 1 ngrx angular

我有一个使用Angular 6和NgRX 6的角度应用程序。

我有一个身份服务器,通过隐式流程与之对话,一旦重定向回我的应用程序,我的令牌响应等仍在导航栏中。

我想从网址中清除该片段,并继续沿我的路线行驶。

例如

http://foo.com/bar/boo#key=value
Run Code Online (Sandbox Code Playgroud)

完成并离开后,我想清除此问题

http://foo.com/bar/boo
Run Code Online (Sandbox Code Playgroud)

我正在处理如下效果

 @Effect({ dispatch: false })
  persistSessionTokens$: Observable<void> = this.actions$.pipe(
    ofType<ActionWithPayload>(SET_SESSION_TOKENS),
    map(action => action.payload),
    tap(tokens => {
      persist(ID_TOKEN_STORAGE_KEY, tokens[ID_TOKEN_STORAGE_KEY]);
      persist(ACCESS_TOKEN_STORAGE_KEY, tokens[ACCESS_TOKEN_STORAGE_KEY]);

      const url = this.router.url;

      this.router.navigate([url], { replaceUrl: true });
    }),
    catchError(error => of({ type: SET_TOKEN_FAILURE, payload: error }))
  );
Run Code Online (Sandbox Code Playgroud)

但这将我重定向到 http://foo.com/bar/

在这种情况下,我的应用程序的基本href为 bar

boo 是我的房客的名字,因此在URL中保留此结构很重要。

Efe*_*Efe 5

您可以使用中的Location@angular/common。这是示例:

// This will give you current location path without including hash
// path(includeHash: boolean = false): string
const pathWithoutHash = this.location.path(false); 

// This changes the browsers URL to the normalized 
// version of the given URL, and replaces the 
// top item on the platform's history stack.
// replaceState(path: string, query: string = '', state: any = null): void
this.location.replaceState(pathWithoutHash);
Run Code Online (Sandbox Code Playgroud)

有关更多 Location