3 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個贊
我實(shí)現(xiàn)的一種可能的解決方案是在沒有參數(shù)的情況下在路由器本身中創(chuàng)建重定向,并將其重定向到本地。
{ path: 'is-protected', redirectTo: 'home', pathMatch: 'full' }
如果查詢參數(shù)不存在,這將處理。這種方法對我有用,因?yàn)槲覜]有任何類似的路線或沒有參數(shù)的路線。
現(xiàn)在在路線守衛(wèi)中
if (state.url.indexOf('is-protected') > -1) {
//this route will have the query param. If it didnt, it would be redirected automatically because of redirection added in list of routes
if (condition I need to test) {
return true
} else {
this.route.navigate(['/home']);
}
}
如果有更好的方法或解決方案,請發(fā)表評論。謝謝!

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個贊
您應(yīng)該注入構(gòu)造函數(shù)路由:ActivatedRouteSnapshot
您可以像這樣訪問路線參數(shù),并將其與您的期望值進(jìn)行比較
let id;
if(route.paramMap.has('id')){
assessmentId = route.paramMap.get("id");
}
添加回答
舉報