在Angular组件和非Angular组件之间进行通信:从可观察到的回调中返回数据

添宝773

我有一个应用程序,要求某些情况下使用非Angular JavaScript。为了触发Angular组件中的动作,我将向下回调传递给非Angular组件。触发回调后,可观察对象将在Angular组件上运行(执行http调用)。这行得通,但我唯一遇到的难题之一就是以某种方式使从该可观察到的返回的数据传递回非Angular组件。我的实际应用程序相当复杂,因此我为简化得多的版本创建了Stackblitz,以使其更容易查看自己在做什么。

这对我来说很棘手,因为GET调用doStuff是异步的,所以我不能只返回结果。我对如何在纯Angular应用程序中解决此问题有一些想法...但是我不确定在Angular组件和非Angular组件之间共享数据时如何实现这一点。

app.component.ts:

 export class AppComponent  {

  constructor(private http: HttpClient){}

  doStuff() {
    let randomNum = this.getRandomInt(2); // Simulate different http responses

    this.http.get<any>(`https://jsonplaceholder.typicode.com/todos/${randomNum}`).subscribe(x => {
      if (x === 1) {
        // Here is where I want to share data with the non-Angular component
        console.log(x.id);
      } else {
        // Here is where I want to share data with the non-Angular component
        console.log(x.id);
      }
    });
  }

  ngOnInit() {
   var x = new NonAngularComponent(this.doStuff.bind(this));
  }

  private getRandomInt(max) {
    return Math.floor(Math.random() * Math.floor(max)) + 1;
  }
}

NonAngularComponent.ts:

export class NonAngularComponent {
  constructor(private onSave: () => void) {
    this.init()
  }

  init() {
    const newElement = document.createElement('button');
    newElement.innerHTML = 'Click';

    newElement.addEventListener('click', () => {
      this.onSave(); // Works, but now I need to do something with the results of doStuff()
    });

    document.getElementById('foo').append(newElement);
  }
}

任何建议将不胜感激。

yurzui

如果要在Angular组件中产生一些副作用,最好ObservabledoStuff()方法中返回并使用tapoperator:

doStuff() {
  let randomNum = this.getRandomInt(2);

  return this.http.get<any>(`https://jsonplaceholder.typicode.com/todos/${randomNum}`).pipe(tap(x => {
    if (x === 1) {
      // Here is where I want to share data with the non-Angular component
      console.log(x.id);
    } else {
      // Here is where I want to share data with the non-Angular component
      console.log(x.id);
    }
  }));
}

非角度组件

newElement.addEventListener('click', () => {
  this.onSave().subscribe(res => {
    // do whatever you want
  });
});

分叉的Stackblitz

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular5从可观察到组件获取数据

订阅Angular 8中可观察到组件的问题

通过服务可观察到的故障在组件之间进行通信

Angular2可观察到的回调

Angular 2无法在可观察到的回调中访问它

Angular 2组件中的单元测试“成功”和“错误”可观察到的响应

在Angular2中可观察到的组件属性变化

可观察到的服务在组件NgOnInIt中不起作用-Angular2

将可观察到的数据从父组件传递给在Angular 2+中使用componentFactoryResolver创建的子组件

可观察到的Redux:如何从回调返回操作?

如何在Angular Jasmine Spec中测试主题和可观察到的返回值?

在Rx中可观察到回调

返回可观察到的内部可观察到的可激活Angular 4防护

如何将数据从可观察到的主题推送到组件中的数组?

在组件之间共享可观察对象/在Angular2中多播可观察对象

如何在Angular2中的主组件和细节组件之间进行内部通信?

Angular forEach循环可观察到的返回数据(Firebase驱动)

可观察到的Angular服务返回嵌套数组数据

Angular 2缓存可观察到的http结果数据

可观察到的Angular和Cloud Firestore无法完成

使用RxJS过滤单个值并返回可观察到的值,并通过Angular模板中的async使用可观察到的值

Angular HTTP可观察到的元组

Angular 组件之间的通信

在同级组件Angular 2之间进行通信

可观察到的rxJs回调是异步的

当您进行返回可观察到的HTTP调用时,如何在Angular单元测试中使用fakesAync?

如何在React中正确进行GET调用以返回可观察到的值(类似于Angular中的方法,而不使用promises)?

Angular 1.5非父组件到组件的通信

从敲除3.2中的自定义组件可观察到的更新