第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Angular 組件和非 Angular 組件之間的通信:從來(lái)自回調(diào)的可觀察對(duì)象返回?cái)?shù)據(jù)

Angular 組件和非 Angular 組件之間的通信:從來(lái)自回調(diào)的可觀察對(duì)象返回?cái)?shù)據(jù)

瀟瀟雨雨 2022-12-18 18:59:20
我有一個(gè)應(yīng)用程序要求我為某些事情使用非 Angular JavaScript。為了在 Angular 組件中觸發(fā)一個(gè)動(dòng)作,我將一個(gè)回調(diào)傳遞給非 Angular 組件。觸發(fā)回調(diào)時(shí),可觀察對(duì)象會(huì)在 Angular 組件上運(yùn)行(執(zhí)行 http 調(diào)用)。這行得通,但我遇到的唯一難題是以某種方式從該可觀察對(duì)象返回的數(shù)據(jù)傳遞回非 Angular 組件。我的實(shí)際應(yīng)用程序相當(dāng)復(fù)雜,因此我創(chuàng)建了一個(gè)簡(jiǎn)化得多的Stackblitz版本,以便更容易看到我在做什么。這對(duì)我來(lái)說(shuō)很棘手,因?yàn)镚ET調(diào)用doStuff是異步的,所以我不能只返回結(jié)果。我對(duì)如何在純 Angular 應(yīng)用程序中解決這個(gè)問(wèn)題有一些想法......但我不確定在 Angular 組件和非 Angular 組件之間共享數(shù)據(jù)時(shí)如何實(shí)現(xiàn)這一點(diǎn)。應(yīng)用程序組件.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;  }}非角度組件.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);  }}任何建議將不勝感激。
查看完整描述

2 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果您想在 Angular 組件中產(chǎn)生一些副作用,最好Observable從您的doStuff()方法返回并使用運(yùn)算符:tap


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);

    }

  }));

}

非角度.component.ts


newElement.addEventListener('click', () => {

  this.onSave().subscribe(res => {

    // do whatever you want

  });

});

分叉的 Stackblitz


查看完整回答
反對(duì) 回復(fù) 2022-12-18
?
開(kāi)心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

我認(rèn)為最簡(jiǎn)單的解決方案是在 AppComponent 中簡(jiǎn)單地?fù)碛幸粋€(gè) NonAngularComponent 實(shí)例


this.nonAngularComponent = new NonAngularComponent(this.doStuff.bind(this));

在回調(diào)中,只需從 NonAngularComponent 中調(diào)用您想要的方法,如下所示:


doStuff() {

    let randomNum = this.getRandomInt(2);

    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);

          this.nonAngularComponent.doSomething(x);

        } else {

          // Here is where I want to share data with the non-Angular component

          // console.log(x.id);

          this.nonAngularComponent.doSomething(x);

        }

      });

  }

doSomething方法:


  public doSomething(result) {

    console.log("Non-Angular component received result", result);

  }

和控制臺(tái)輸出:

http://img1.sycdn.imooc.com//639ef2b1000194f605850133.jpg

查看完整回答
反對(duì) 回復(fù) 2022-12-18
  • 2 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)