我有 2 個具有父子關系的組件。在父組件中,我有圖像,單擊這些圖像應導航到子組件。以下是我的代碼,瀏覽器中的 URL 正在更改但頁面未導航。路由const routes: Routes = [ { path: 'parent', component: ParentComponent, children: [ { path: 'child', component: ChildComponent} ] }, { path: '**', component: LoginComponent }];HTML<section> <img src="imagePath" alt="" (click)="gotoProfile()"></section><div><router-outlet></router-outlet></div>TSgotoProfile() { this.route.navigate(['/parent/child']);}只有當我使用布爾變量在按鈕單擊時顯示隱藏(這不是一個好習慣)時,導航才有效,如下所示。導航后使用布爾值會引發一些問題,在子組件中單擊后退按鈕時父組件未加載。TS gotoProfile() { this.hideParentDiv = true; this.route.navigate(['/parent/child']); }HTML <section *ngIf="hideParentDiv "> <img src="imagePath" alt="" (click)="gotoProfile()"> </section> <div *ngIf="!hideParentDiv "> <router-outlet></router-outlet> </div>誰能幫我解決這個問題,非常感謝任何幫助。
父子之間的 Angular 導航路由
紅糖糍粑
2023-04-01 15:43:37