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

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

滾動到另一個組件的特定標簽

滾動到另一個組件的特定標簽

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>

結帳角度片段


查看完整回答
反對 回復 2022-12-22
?
烙印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();

        }

    });

}


查看完整回答
反對 回復 2022-12-22
  • 2 回答
  • 0 關注
  • 73 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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