Passing value between two angular2 component typescript files

妖精的绣舞 提交于 2019-12-01 10:36:27

问题


I have two components that are not parent and child components but i need to pass value from component A to component B.

example:

src/abc/cde/uij/componentA.ts has variable CustomerId = "ssss"

need to pas that variable customerID to src/abc/xyz/componentB.ts


回答1:


Simple example:

Component A:

@Component({})
export class ComponentA {
    constructor(private sharedService : SharedService) {}

    sendMessage(msg : string) {
       this.sharedService.send(msg);
    }
}

Component B:

@Component({})
export class ComponentB {
    constructor(private sharedService : SharedService) {
       this.sharedService.stream$.subscribe(this.receiveMessage.bind(this));
    }

    receiveMessage(msg : string) {
       console.log(msg); // your message from component A
    }
}

Shared service:

@Injectable()
export class SharedService {
    private _stream$ = new Rx.BehaviorSubject("");
    public stream$ = this._stream$.asObservable();

    send(msg : string) {
      this._stream$.next(msg);
    }
}

Shared service have to be placed in the same NgModule.




回答2:


On your service define a setMyProperty() and a getMyProperty(). Then setMyProperty with a value from Component A. ComponentB then getMyProperty where you return the value...

You have to inject the service into both components.




回答3:


You could give this a shot. Its pretty simple and straight forward.

I just followed THIS example and made some changes so that it could be siblings talking instead of parent/child.

my-service.service.ts

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class MyService {
  // Observable string sources
  private myAnnouncedSource = new Subject<string>();

  // Observable string streams
  myAnnounced$ = this.myAnnouncedSource.asObservable();

  // Service message commands
  announceItem(item: string) {
    this.myAnnouncedSource.next(item);
  }
}

my-comp1.component.ts

import { Component }          from '@angular/core';
import { MyService }     from './my-service.service';
@Component({
  selector: 'my-compA',
  template: `...`,
  providers: [MyService]
})
export class MyComponentA {

  constructor(private myService: MyService) {

  }
  announceToOtherComps() {
    let sharedItem = "shibby";
    this.myService.announceItem(sharedItem);
  }
}

my-comp2.component.ts

import { Component, Input, OnDestroy } from '@angular/core';
import { MyService } from './my-service.service';
import { Subscription }   from 'rxjs/Subscription';
@Component({
  selector: 'my-compB',
  template: `...`,
  providers: [MyService]
})
export class MyComponentB implements OnDestroy {

  sharedItem = '<no data>';
  subscription: Subscription;

  constructor(private myService: MyService) {
    this.subscription = myService.myAnnounced$.subscribe(
      item => {
        this.sharedItem = item;
    });
  }

  ngOnDestroy() {
    // prevent memory leak when component destroyed
    this.subscription.unsubscribe();
  }
}



回答4:


<component-a [id]="product.id"></component-a>

In the component-a ts file .Use it like below

export class ComponentA implements OnInit {

@Input() // <------------
 id: number;

 (...)
}


来源:https://stackoverflow.com/questions/39981545/passing-value-between-two-angular2-component-typescript-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!