简体   繁体   English

在同级组件Angular 5之间传递对象{}

[英]Passing object { } between sibling components Angular 5

I'm not sure what I'm doing wrong here... 我不确定我在做什么错...

Basically I have 2 sibling components the first component gets some data and I want to share that data with the sibling component so I have made a service 基本上,我有2个同级组件,第一个组件获取一些数据,我想与同级组件共享该数据,因此我已经提供了服务

service 服务

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

@Injectable()
export class DataService {
    // Individual Data Source
    private dataSource = new BehaviorSubject<Object>({});
    // Individual Data Source
    data = this.dataSource.asObservable();
    // Individual Data Source
    updateData(data) {
        this.dataSource.next(data);
    }
}

then in my first component... 然后在我的第一个组件中...

this.activatedRoute.params.subscribe(params => {
  this.id = params.id;
  this._dataNotification.getSingledataNotification(this.id)
    .subscribe(result => {
      this.data = result;
      this._dataService.updateData(this.data);
    });
});

then in my second component.. 然后在我的第二部分。

getData() {
    this._dataService.data.subscribe((result) => {
        this.data = result;
        console.log(this.data);
    });
}

basically what Is happening is in my first component im getting data object back and passing it to the updateData function then In my second component Im getting that data but when I console log I only get back an empty object?? 基本上发生了什么事是在我的第一个组件中获取数据对象并将其传递给updateData函数,然后在我的第二个组件中获取数据,但是当我控制台日志时,我仅获取了一个空对象?

any ideas what I'm doing wrong? 有什么想法我做错了吗?

EDIT 编辑

I put the updateData function in a onChanges lifecycle event and now It works.. 我将updateData函数放入onChanges生命周期事件中,现在它可以工作了。

Thanks Guys 多谢你们

This one creates a service to handle the message events. 这个创建了一个服务来处理消息事件。

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

@Injectable()
export class MessageService {
  private subject = new Subject<Message>();

  send(key: string, value: any) {
    this.subject.next({ key, value });
  }

  get(): Observable<Message> {
    return this.subject.asObservable();
  }
}

class Message {
  constructor(
    public key: string,
    public value: any) { }
}

This one listen to the events and when get the events execute sth. 这个侦听事件,并在事件得到执行时执行。

import { Subscription } from 'rxjs/Subscription';
private subscription: Subscription;

constructor(
  private messageService: MessageService,
) {}

ngOnInit() {
  this.subscription = this.messageService.get().subscribe(message => {
    if (message.key === 'what message you want') {
      this.dosth()
    }
  });
}

When you want to send the object from one service to another service, call the send in the message service and subscribe in the service you want. 当您要将对象从一项服务发送到另一项服务时,请调用消息服务中的send并订阅所需的服务。

I faced the similar problem when I had to implement the identical scenario. 当我不得不实施相同的方案时,我遇到了类似的问题。

The solution to it is that, you should not receive the data in the receiving component inside the onInit() method. 解决方案是,您不应在onInit()方法内的接收组件中接收数据。 Rather, receive it through an onChanges() method or through an event (like a button click ). 而是通过onChanges()方法或通过事件(如按钮click )接收它。 It will return you the object with values in it. 它将返回带有值的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM