當點擊瀏覽器后退按鈕時,應用程式重定向到應用程式的第一個 url。例如,當前路線是localhost:3000然后我要回家localhost:3000/home然后到localhost:3000/settings,當我單擊瀏覽器后退按鈕時,它會將我重定向到localhost:3000而不是localhost:3000/home.
我正在使用 angular 14,但是當我開始構建應用程式時,angular 是 13 版。這是app.module.ts:
@NgModule({
declarations: [AppComponent],
imports: [
...
BrowserModule,
AppRoutingModule,
],
providers: [
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
...
],
bootstrap: [AppComponent]
})
這是app-routing.module.ts:
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, onSameUrlNavigation: "reload" }),
BrowserAnimationsModule
],
exports: [RouterModule]
})
路線都是這樣的:
{
path: "user/profile",
loadChildren: () => import("./user/profile/profile.module").then(m => m.ProfilePageModule),
canActivate: [LoggedGuard],
},
這是到處用于導航的導航功能:
async go(path: string[], options: NavigationExtras = {}, showLoader: boolean = true) {
if(showLoader === true) {
await this.loader.start();
}
this.router.navigate(path, { replaceUrl: true, ...options});
}
uj5u.com熱心網友回復:
如果您共享一個重現您的問題的 stackblitz 專案會更容易。
也許這是與路線定義有關的問題。您需要將路由設定為默認頁面,在您的情況下是主頁
const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
path: 'home',
loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)
},
{
path: 'profile',
loadChildren: () => import('./profile/profile.module').then( m => m.ProfilePageModule)
},
];
uj5u.com熱心網友回復:
我剛剛想通了。問題中的函式go()具有替換 url 并且不將 url 保存到瀏覽器歷史記錄的功能,這意味著如果您單擊瀏覽器后退按鈕,它會按您希望的方式作業。設定將 url 保存到瀏覽器歷史記錄和瀏覽器后退按鈕將正常作業。replaceUrl: truereplaceUrl: false
這就是函式現在的樣子:
async go(path: string[], options: NavigationExtras = {}, showLoader: boolean = true) {
if(showLoader === true) {
await this.loader.start();
}
this.router.navigate(path, { replaceUrl: false, ...options});
}
還有一點,請盡量使用<a routerLink="path"></a>。這樣,當用戶使用滑鼠中鍵(滾輪)單擊鏈接時,頁面將在新選項卡中打開。按鈕不能這樣作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525804.html
