简体   繁体   English

如何将数据从 Subject observable 推送到组件中的数组?

[英]How do I push data from a Subject observable to an array in the component?

I am trying to push a message into an array that is already declared as a variable in the component.我正在尝试将消息推送到已在组件中声明为变量的数组中。 I am using a service and have created a subject observable to take data from one component and inject it into another component.我正在使用一项服务并创建了一个可观察的主题,以从一个组件中获取数据并将其注入另一个组件。 When I try to push the data onto the array after subscribing to the variable, it's updated temporarily but when I open that component, the data is not pushed.当我在订阅变量后尝试将数据推送到数组中时,它会临时更新,但是当我打开该组件时,不会推送数据。 The array updates when I console log from inside the subscribe method but it's reset once I open that component.当我从 subscribe 方法内部控制台日志时,数组会更新,但一旦我打开该组件,它就会重置。 I don't know what is the problem.我不知道是什么问题。 This is the code:这是代码:

Service.ts服务.ts

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

@Injectable({
  providedIn: 'root'
})
export class SerService {

  private message = new Subject<string>();

  sourceMessage$ = this.message.asObservable();

  constructor() { }

  sendMessage(message: string) {
    this.message.next(message);
  }
}

Receiver component接收器组件

import { Component, OnInit } from '@angular/core';
import { SerService } from '../ser.service';
import { User } from "../user";

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  public messages = ['hi', 'hello', 'bye'];

  constructor(private _service: Service) { }

  ngOnInit() {
    this._service.message$
      .subscribe(
        message => {
          this.messages.push(message);
        }
      );
  }
}

Sender Component发件人组件

import { Component, OnInit } from '@angular/core';
import { SerService } from '../ser.service';
import { User } from '../user';

@Component({
  selector: 'app-sign-up',
  templateUrl: './sign-up.component.html',
  styleUrls: ['./sign-up.component.css']
})
export class SignUpComponent {

  userModel = new User('', '', '', '', false);

  constructor (private _service : SerService) {}

  onSubmit(){
      this._service.sendMessage(this.userModel.message);
  }

}

I can't update the message array.我无法更新消息数组。 How do I do this with minimal changes?如何以最少的更改做到这一点?

You can create a service to send data from one component to another by using BehaviourSubject您可以使用 BehaviourSubject 创建服务以将数据从一个组件发送到另一个组件

Service:服务:

 import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; @Injectable({ providedIn: 'root' }) export class DataService { private userDetails = new BehaviorSubject<any>(''); currentUserDetails = this.userDetails.asObservable(); constructor() { } sendUserDetails(message){ this.userDetails.next(message) } }

Sender Component:发件人组件:

 import { DataService } from '/services/data.service'; export class SignupComponent implements OnInit { public userDetails; constructor(private _dataService: DataService) {} ngOnInit(){ userDetails = new User('', '', '', '', false); this._dataService.sendUserDetails(this.userDetails); } }

Receiver Component接收器组件

 import { DataService } from '/services/data.service'; export class LoginComponent implements OnInit { public userDetails; constructor(private _dataService: DataService) {} ngOnInit(): void { this._dataService.currentUserDetails.subscribe(userDetails => this.userDetails = userDetails); }

Blockquote块引用

暂无
暂无

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

相关问题 如何在 Storybook 中使用 observable 或 subject 更新 Angular 组件? - How can I update Angular component with observable or subject in Storybook? 如何使用 array.push() 方法,以便将数据从一个组件推送到单独文件中的数组中? - How do I can I use the array.push() method, so I can push data from one component into an array in a seperate file? 如何正确处理依赖于ViewChild和Angular 2中可观察数据的组件上的更改检测? - How do I properly handle change detection on a component that relies on a ViewChild and data from an observable in Angular 2? 如何在 observable 上对我的数据数组进行排序? - How do I sort my array of data on an observable? 如何在 Angular 组件中显示来自 Observable 的数据 - How to display data from Observable in Angular component 如何从 observable 中正确获取数组数据? - How to properly get array data from observable? 我如何将数据从组件A发送到组件B Angular 2 - How do i send data from component A to component B Angular 2 如何在 Angular 中将数据从一个组件发送到另一个组件? - How do I send data from a Component to another Component in Angular? 如何从主ViewModel更新组件内部的可观察数组? - How to update an observable array inside a component from the main ViewModel? 我如何用承诺返回的数组替换敲除js可观察数组的内容 - How do I replace the contents of knockout js observable array with an array returned from a promise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM