[英]Angular Service dependency injection on same component from different routes
我有兩個模塊,如下所示。 我想在第二個模塊中共享來自第一個模塊的組件,但帶有一個參數來指示它來自哪個模塊。 我怎樣才能使用服務做到這一點?
第一個模塊
@NgModule( {
declarations: [
MasterListComponent
]
在第二個模塊中,我可以輕松地使用如下組件
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'list'
}, {
path: 'list',
data: { title: 'Administration' },
component: MasterListComponent
}];
但是在 MasterListComponent 構造函數中,我如何注入服務來知道從哪里調用了組件?
您可以像這樣直接將屬性添加到模塊中:
{
path: 'list',
data: {
title: 'Administration',
module: 'Your module',
},
component: MasterListComponent
}
然后在您的組件中執行以下操作:
constructor(private route: ActivatedRoute) {}
ngOnInit(): void {
console.log(this.route.snapshot.data.module);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.