这是路由.ts
{
path: 'adminHome',
component: adminHomeComponent,
children: [
{
path: 'users',
component: UserListComponent,
children: [
{
path: ':id',
component: EntrepriseListComponent,
children: [
{
path: ':id2',
component: ListLaucauxComponent,
children:[
{
path:':id3',
component:DeviceListComponent }
].......
这是 UserListComponent ,其中包含用户和路由器出口列表以显示 EntrepriseListComponent
@Component({
template :`
<h2> List of users</h2>
<ul *ngFor=" let user of users >
<li>{{user.firstName}}</li>
<li>{{user}}</li>
<li><a [routerLink]="['/adminHome/users',user.id]">L.E</a></li>
</ul>
<router-outlet></router-outlet> `
})
export class UserListComponent implements OnInit {
private users: User[];
constructor(private userService: UserService ) { }
ngOnInit()
{
this.userService.getAllUsers().subscribe(data => {this.users = data} )
}
这是 EntrepriseListComponent,其中包含 Entreprise 和路由器出口列表以显示 LocalListComponent
@Component({
template:`
<h2>List of entreprise </h2>
<ul *ngFor="let entreprise of entreprises">
<li>{{entreprise .id}}</li>
<li>{{entreprise .name}}</li>
<li><a [routerLink]="
['/adminHome/users/',idUser,entreprise.id]">L.L</a></li>
</ul>
<router-outlet></router-outlet> `
})
export class EntrepriseListComponent implements OnInit {
constructor(private Service:EntrepriseService ,private
route:ActivatedRoute) { }
entreprises : Entreprise[];
idUser :string ; // this what i want to get from parent
ngOnInit() {
this.route.params.forEach(params=>{
this.route.params.forEach(params => {
let id = params['id'];
this.entrepriseService.getEntrepriseByIdUser(id)
.subscribe(data => {
console.log(data)
this.entreprises = data;
})
this.sharedService.userId$.subscribe(data => this.userId = data);
this.sharedService.updateUserId(id);
})
}
这是 LaucauxListComponent,其中包含 laucaux 和路由器出口的列表以显示 DeviceListComponent
@Component({
template: `
<h2>List des Locaux </h2>
<ul *ngFor="let x of laucaux">
<li>{{x.name}}</li>
<li><a [routerLink]="['/adminHome/users/',idUSer,idEntreprise,x.id]">Liste
Devices</a></li>
</ul>
<router-outlet></router-outlet>`
})
export class ListLaucauxComponent implements OnInit {
laucaux: Laucaux[]
constructor(private ls: LoacauxService, private route:ActivatedRoute) { }
idUSer :string ;// this what i want to get from parent
idEntreprise : string ;// this what i want to get from parent
ngOnInit() {
let id = params['id2'];
this.loacauxService.getLaucauxByEntreprise(id)
.subscribe(data => {
console.log(data)
this.laucaux = data;
})
//retrieve values
this.sharedService.userId$.subscribe(data => this.iduser = data);
this.sharedService.enterpriseId$.subscribe(data => this.identreprise
= data);
//update values
this.sharedService.updateUserId(this.iduser);
this.sharedService.updateUserId(this.identreprise);
}
那么如何在 EntrepriseListComponent 中获取 idUSer 和在 ListLaucauxComponent 中获取 idUSer&idEntreprise