1

这是路由.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

4

1 回答 1

0

当像这样跨路线共享数据时,最好创建一个共享服务供他们使用。每个组件都可以更新共享服务中的任何相关数据,您可以使用每个组件可以订阅的 Observables 来接收共享数据的更新。它看起来像这样:

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';
@Injectable()
export class SharedService {
  // Observable string sources
  private userIdSource = new Subject<string>();
  private enterpriseIdSource = new Subject<string>();

  // Observable string streams
  userId$ = this.userIdSource.asObservable();
  enterpriseId$ = this.enterpriseIdSource.asObservable();

  // Service message commands
  updateUserId(id: string) {
    this.userIdSource.next(id);
  }
  updateEnterpriseId(id: string) {
    this.enterpriseIdSource.next(id);
  }
}

通过使用Subject,组件将仅接收组件订阅后更新的值。IE:SharedService 发送一个新的userIdTHEN ComponentA 订阅userId$observable。ComponentA 不会获得先前更新的值。如果您需要将任何先前的值发送到组件而不管它们何时订阅,请使用BehaviorSubject而不是Subject.

组件将订阅值并更新值,如下所示:

export class ComponentA {

    userId:string;

    constructor(private sharedService: SharedService){}


    ngOnInit(){
        //retrieve values
        this.sharedService.userId$.subscribe(data => this.userId = data);

        //update values
        this.sharedService.updateUserId('newValue');
    }
}

希望这可以帮助。

于 2017-04-08T15:15:58.213 回答