[英]reloading the current component (with (click)) if the routerlink active is the same of component actual
我希望當我單擊鏈接時,它將帶我到"/" route
,這可行。
<a routerLink="/">Login</a>
但是,如果我已經在"/"
路線上,則希望為其充電的當前組件被充電。 換句話說,如果我在"/"
路徑上,並且單擊該標簽<a>
,則希望重新加載視圖。 我該怎么做?
如果您在同一條路線上並嘗試對其進行重新導航,則默認情況下,angular 不會再次對您進行重新布置。 這樣做主要是出於性能方面的考慮。 (IMO,這很有意義)。
相反,如果您想實現相同的功能,則可以調用ngOnInit()
/ method,該方法為同一組件加載數據。
html的
<a routerLink="/" (click)="reloadData()">Reload</a>
.TS
reloadData() {
this.ngOnInit();
// or another method that loads data for the component
// this.loadData();
}
這種方法的優點是您可能不想重新導航到當前所在的所有頁面。 因此,這將根據您的要求在組件之間特定。
修改您的根NgModule
裝飾器,如下所示:
@NgModule({
......
imports: [
.....,
RouterModule.forRoot(routes, { onSameUrlNavigation: 'reload' }),
......
],
......
})
export class AppModule {
.......
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.