3 回答

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊
您需要使用導(dǎo)航選項(xiàng)。當(dāng)您收到導(dǎo)航到錯(cuò)誤頁面的錯(cuò)誤時(shí),應(yīng)該使用NavigationExtras來實(shí)現(xiàn)。您可以在那里定義skipLocationChange或replaceUrl。
skipLocationChange 在導(dǎo)航更改時(shí)跳過以將狀態(tài)推送到歷史記錄
replaceUrl 重新定義歷史中的當(dāng)前狀態(tài)
代碼應(yīng)如下所示:
this.router.navigate(['/error'], { replaceUrl: true });
文檔參考:https : //angular.io/api/router/NavigationExtras

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
Angular 路由保護(hù) CanActivate 方法將幫助您解決我們的問題,當(dāng)用戶導(dǎo)航或嘗試從一個(gè)屏幕導(dǎo)航到另一個(gè)屏幕時(shí)它會觸發(fā)。它還將為您提供可以根據(jù)您的要求輕松操作的下一個(gè) url 路由。您只需在提供程序中包含路由保護(hù)并實(shí)現(xiàn) CanActivate 路由保護(hù)。
step 1-實(shí)現(xiàn)CanActivate接口
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
// here you can write you logic example-> you can navigate to some where else protect navigation
console.log('AuthGuard#canActivate called');
return true;
}
}
第 2 步 -> 告訴你的路由表你有一個(gè)守衛(wèi)來保護(hù)你的路由:P
import { AuthGuard } from '../auth/auth.guard';
const adminRoutes: Routes = [
{ path: 'crises', component: ManageCrisesComponent },
{ path: 'admin', component: AdminComponent, canActivate: [AuthGuard]} // -->your Auth guard will get register here
];

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
對我來說,后退按鈕是瀏覽器和用戶的問題。我將解釋我自己,如果用戶想回去,你應(yīng)該讓他們回去,這對我來說是正確的方式,通常的流程以及應(yīng)該如何完成。
但為了實(shí)現(xiàn)這一點(diǎn),您可以這樣做:
在您的錯(cuò)誤頁面中,您可以使用 history.pushState 文檔:https : //developer.mozilla.org/en-US/docs/Web/API/History/pushState
history.pushState(null, null, window.location.href);
添加回答
舉報(bào)