我有一個稱爲home的路由,它有三個子路由,文檔,郵件和垃圾。在home路由組件中,它有一個名爲'user'的變量。我知道有幾種方法在突出顯示here的父組件和子組件之間傳遞信息,但我如何在父/子路徑之間傳遞信息。angular2 - 將父值路由傳遞給子路由
{ path: 'home', component: HomeComponent, children: [
{ path: 'documents', component: DocumentsComponent },
{ path: 'mail', component: MailComponent },
{ path: 'trash', component: TrashComponent },
]
},
服務
import { Injectable } from '@angular/core';
@Injectable()
export class HomeService {
// Mock user, for testing
myUser = {name:"John", loggedIn:true};
// Is Super Admin
isLogged():boolean {
if(this.myUser.role == true){
return true ;
}
return false ;
}
}
組件
constructor(public router: Router, public http: Http, private homeService: HomeService) {
}
isLogged(){
return this.homeService.isLogged();
}
模板
<div class="side-nav fixed" >
<li style="list-style: none">
<img alt="avatar" class="circle valign profile-image" height="64" src=
"../images/avatar.jpg" width="64">
<div class="right profile-name">
<!-- Value not changing even with service -->
{{myUser.role}}
</div>
</li>
我 – John
不工作,不爲你在工作,你是不是能夠實現這一點,或者你有一些不同的場景?如果情況不同,請添加更多細節。 –
在我的html模板中,我有一個ngIf來檢查用戶是否登錄。但是,它沒有讀取檢查的服務方法。 – John