简体   繁体   English

BehaviorSubject 不发出值

[英]BehaviorSubject doesn't emit values

i have a problem with emiting values from behavior subject after switchMap operator from parent to child component.在 switchMap 运算符从父组件到子组件之后,我从行为主体发出值时遇到问题。 If i call real http API in console.log in child compoennt i only see empty array [] (default value), but in tap operator in parent component if i console.log data i saw array with 20 items, but in child component not.如果我在子组件的console.log中调用真正的http API,我只能看到空数组[](默认值),但如果我在console.log数据中看到父组件中的点击运算符,我看到包含20个项目的数组,但在子组件中没有. When i tried to make a mock service and return mocked data.当我尝试制作模拟服务并返回模拟数据时。

eg.例如。 return of(['item1', 'item2']

This case works fine, but when i only switched call service name, it doesn't work correctly for me, in tap i see data, but in child input not.这种情况下工作正常,但是当我只切换呼叫服务名称时,它对我来说不能正常工作,在点击中我看到数据,但在子输入中没有。

import { Component, VERSION } from '@angular/core';
import { BehaviorSubject, Subject } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { AppserviceService } from './appservice.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
  refresh$: Subject<void> = new Subject();
  data$: BehaviorSubject<string[]> = new BehaviorSubject<string[]>([]);

  constructor(private _appService: AppserviceService) {
    this.refresh$
      .pipe(switchMap(() => this._appService.test2()))
      .subscribe(res => {
        console.log('Subscribe:');
        console.log(res);
        this.data$.next(res)
      });

    this.refresh$.next();
  }
}


Child component子组件

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Component({
  selector: 'hello',
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <h1>Hello {{ name }}!</h1>
  `,
  styles: [
    `
      h1 {
        font-family: Lato;
      }
    `
  ]
})
export class HelloComponent {
  @Input() name: string;
  @Input() set test(value: BehaviorSubject<any[]>) {
    console.log(value.getValue());
  }
}

https://stackblitz.com/edit/angular-ivy-7wezwh?file=src/app/app.component.ts https://stackblitz.com/edit/angular-ivy-7wezwh?file=src/app/app.component.ts

Did you met with this issue?你遇到过这个问题吗? Thanks.谢谢。

The reason is your child component gets instantiated before your http call completes.原因是您的子组件在您的 http 调用完成之前被实例化。 Right now you are only getValue() ing the first value.现在你只是getValue() ing第一个值。 If you want the BehaviorSubject to emit new data every time it has one, you can fix this by simply adding the async operator:如果您希望BehaviorSubject每次有新数据时都发出新数据,您可以通过简单地添加 async 运算符来解决此问题:

<hello name="{{ name }}" [test]="data$ | async"></hello>
@Input() set test(value: any[]) {
  console.log(value);
}

Now every time the BehaviorSubject has a new value, it will be emitted to your child component现在每次BehaviorSubject有一个新值时,它都会被发送到你的子组件

Fixed stackblitz 固定堆栈闪电战

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

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