为什么 queryparamshandling: "preserve" 没有按预期工作?
Why queryparamshandling: "preserve" is not working as expected?
朋友们,我被 queryParamsHandling 参数困住了,它没有按预期工作。
在我的应用程序中,我有一个 重定向服务 ,它只是通过使用选择器检查用户角色,并根据角色重定向到特定的 url。代码如下。我想要的只是保存 queryParams 并将它们重定向到不同的路线上。我发现 queryParamsHandling 就是为此目的。但它总是刷新重定向 URL 上的所有 queryParams。
我想要像这样的初始 url:http://main-page.com?param1=500 after redirect to be http://main-page-unauth.com?param1=500 仍然保留查询参数 1。并且无论有多少 queryParams 都适用 - 1,2,3...1000
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { select, Store } from "@ngrx/store";
import * as fromAuth from "../reducers";
import { map, take, tap } from "rxjs/operators";
import { HttpParams } from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class RedirectGuardService implements CanActivate {
constructor(
private router: Router,
private store: Store<fromAuth.State>,
private activatedRoute: ActivatedRoute
) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.store.pipe(
select(fromAuth.selectRole),
tap((role) => {
if (role)
this.router.navigate([state.url + '-' + role]).then()
else
this.router.navigate([ '/main-unauth'], { queryparamshandling: 'preserve' })
.then()
}),
take(1),
map(() => true)
)
}
}
我什至调试了那段代码,可以清楚地看到何时检查查询参数处理。在那一刻,currentUrlTree 总是 null.
case 'preserve':
q = this.currentUrlTree.queryParams;
break;
如何保存 queryParams 并将其传递给重定向 URL?谢谢
更新: queryparamshandling 在组件中按预期工作(加载应用程序时)。问题是我试图在 canActivate 处理中使用它。在这种情况下我该如何传递 queryParams?
你好像打错了:
努力改变
this.router.navigate([ '/main-unauth', { queryparamshandling: 'preserve' } ])
至
this.router.navigate([ '/main-unauth' ], { queryparamshandling: 'preserve' } )
queryParamsHandling 在 canActivate 守卫期间不工作。如果要保留 queryParams,则需要使用 ActivatedRouteSnapshot
手动传递 queryParams
解决方案有效。而且还有解释。
朋友们,我被 queryParamsHandling 参数困住了,它没有按预期工作。 在我的应用程序中,我有一个 重定向服务 ,它只是通过使用选择器检查用户角色,并根据角色重定向到特定的 url。代码如下。我想要的只是保存 queryParams 并将它们重定向到不同的路线上。我发现 queryParamsHandling 就是为此目的。但它总是刷新重定向 URL 上的所有 queryParams。 我想要像这样的初始 url:http://main-page.com?param1=500 after redirect to be http://main-page-unauth.com?param1=500 仍然保留查询参数 1。并且无论有多少 queryParams 都适用 - 1,2,3...1000
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { select, Store } from "@ngrx/store";
import * as fromAuth from "../reducers";
import { map, take, tap } from "rxjs/operators";
import { HttpParams } from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class RedirectGuardService implements CanActivate {
constructor(
private router: Router,
private store: Store<fromAuth.State>,
private activatedRoute: ActivatedRoute
) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.store.pipe(
select(fromAuth.selectRole),
tap((role) => {
if (role)
this.router.navigate([state.url + '-' + role]).then()
else
this.router.navigate([ '/main-unauth'], { queryparamshandling: 'preserve' })
.then()
}),
take(1),
map(() => true)
)
}
}
我什至调试了那段代码,可以清楚地看到何时检查查询参数处理。在那一刻,currentUrlTree 总是 null.
case 'preserve':
q = this.currentUrlTree.queryParams;
break;
如何保存 queryParams 并将其传递给重定向 URL?谢谢
更新: queryparamshandling 在组件中按预期工作(加载应用程序时)。问题是我试图在 canActivate 处理中使用它。在这种情况下我该如何传递 queryParams?
你好像打错了:
努力改变
this.router.navigate([ '/main-unauth', { queryparamshandling: 'preserve' } ])
至
this.router.navigate([ '/main-unauth' ], { queryparamshandling: 'preserve' } )
queryParamsHandling 在 canActivate 守卫期间不工作。如果要保留 queryParams,则需要使用 ActivatedRouteSnapshot
手动传递 queryParams解决方案有效。而且还有解释。