GCT1015
2022-12-22 15:36:19
如何通過單擊按鈕從一個組件導航到另一個組件。就像我在標題組件中有一個帶有“主頁”、“關于”...的導航欄<li><a class="text-white" href="#home">Home</a></li><li><a class="text-white" href="#about">About</a></li><li><a class="text-white" href="#jobs">Opportunities</a></li><li><a class="text-white" href="#volunteers">Volunteers</a></li><li><a class="text-white" href="#donors">Donors</a></li><li><a class="text-white" routerLink="/contact">Contact</a></li>還有一個像這樣叫做 home 的組件<section id="home"> <app-home-component></app-home-component></section><section id="about"> <app-about></app-about></section><section id="jobs"> <app-jobs></app-jobs></section><section id="volunteers"> <app-volunteers></app-volunteers></section><section id="donors"> <app-donors></app-donors></section>現在,當我單擊導航欄中的關于按鈕時,它應該滑動到主頁組件中的關于組件。我不知道該怎么做。我嘗試使用 id 和 href 但它沒有用。那么有什么建議嗎?
2 回答

當年話下
TA貢獻1890條經驗 獲得超9個贊
使用角度特征片段讓瀏覽器處理它
<a routerLink="" fragment="volunteers"> Volunteers </a>
結帳角度片段

烙印99
TA貢獻1829條經驗 獲得超13個贊
在標頭組件中,將 queryParams 添加到鏈接中:
<li><a class="text-white" [routerLink]="['/home']" [queryParams]="{id: 'home'}">Home</a></li>
<li><a class="text-white" [routerLink]="['/home']" [queryParams]="{id: 'about'}">About</a></li>
在home組件中檢查 queryParams 的路由,如果有任何 id,你應該將它滾動到視圖中:
constructor(private route: ActivatedRoute) {
this.route. queryParams.subscribe(params => {
if (params && params.id) {
let element = document.getElementById(params.id);
element && element.scrollIntoView();
}
});
}
添加回答
舉報
0/150
提交
取消