亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

父子之間的 Angular 導航路由

父子之間的 Angular 導航路由

紅糖糍粑 2023-04-01 15:43:37
我有 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>誰能幫我解決這個問題,非常感謝任何幫助。
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

router -outlet是用于渲染組件的標簽。你不應該隱藏它。這就像在試圖進入之前擋住一扇門。如果你想“隱藏”元素,那么你應該在導航層次結構中向上移動路由器出口。這意味著您應該在路由器中創建“要單擊的圖像頁面”和“詳細圖像頁面”兄弟姐妹。像這樣:


const routes: Routes = [

  {

    path: 'home', component: ExampleComponent, children: [

      { path: 'images-page', component: ImagesComponent },

      { path: 'image-detail-page', component: ImageDetailComponent}

    ]

  },

  { path: '**', component: LoginComponent }

];


查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號