这是Routing.ts如何从角度2中的子组件中的父组件获取变量?
{
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);
}
所以我怎样才能在ListLaucauxComponent获得ID用户在EntrepriseListComponent和ID用户& idEntreprise名单
可能重复的[如何从父母到孩子的组件ID?](http://stackoverflow.com/questions/43292016/how-to-get-the-id-from-parent-to-child-组件) –
它没有应答 –