javascript - Share Data Between Component - Stack Overflow

I want to share data between ponent using service. But It not working as expected.Componentlet myNum =

I want to share data between ponent using service. But It not working as expected.

Component

let myNum = 1;
sendChanges(myNum) {
    this.breadService.sendData$.next(myNum);
}

Service

public sendData$: Subject<any> = new Subject();
public setValue$: BehaviorSubject<any> = new BehaviorSubject(this.data);

Sibling COmponent

ngOnInit() {
    this.breadService.sendData$.subscribe(() => {
        this.breadService.setValue$.subscribe(data=>{
            this.id = data
            console.log(this.id);
        });
    });
}

I want to share data between ponent using service. But It not working as expected.

Component

let myNum = 1;
sendChanges(myNum) {
    this.breadService.sendData$.next(myNum);
}

Service

public sendData$: Subject<any> = new Subject();
public setValue$: BehaviorSubject<any> = new BehaviorSubject(this.data);

Sibling COmponent

ngOnInit() {
    this.breadService.sendData$.subscribe(() => {
        this.breadService.setValue$.subscribe(data=>{
            this.id = data
            console.log(this.id);
        });
    });
}
Share Improve this question edited May 14, 2020 at 19:53 Antoni Silvestrovič 1,0359 silver badges25 bronze badges asked May 14, 2020 at 19:49 PravuPravu 6081 gold badge10 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Here sendData$ is a Subject. Subscription callbacks to a Subject aren't executed till it emits a new value. So the inner subscription wouldn't be executed till a new value is pushed to sendData$ after it is subscribed to. You could change the outer observable too to a BehaviorSubject to assign the value in the subscription immediately.

Service

private sendDataSource: BehaviorSubject<any> = new BehaviorSubject(null);
private setValueSource: BehaviorSubject<any> = new BehaviorSubject(this.data);

public set sendData(data) {
  this.sendDataSource.next(data);
}

public set setValue(value) {
  this.setValueSource.next(value);
}

public get sendData() {
  return this.sendDataSource.asObservable();
}

public get setValue() {
  return this.setValueSource.asObservable();
}

Also a subscription within a subscription isn't elegant. Pipe the outer observable.

Sibling Component

import { pipe } from 'rxjs';
import { switchMap } from 'rxjs/operators';

ngOnInit() {
  this.breadService.sendData.pipe(switchMap(() => this.breadService.setValue))
    .subscribe(data => {
      this.id = data
      console.log(this.id);
    });
}

Seems like you have redundant variable and subscription

Component

let myNum = 1;
sendChanges(myNum) {
    this.breadService.data$.next(myNum);
}

Service

public data$: BehaviorSubject<any> = new BehaviorSubject(this.data);

Sibling ponent

ngOnInit() {
    this.breadService.data$.subscribe((data) => {
        this.id = data
        console.log(this.id);
    });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744236967a4564515.html

相关推荐

  • javascript - Share Data Between Component - Stack Overflow

    I want to share data between ponent using service. But It not working as expected.Componentlet myNum =

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信