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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從提交事件創(chuàng)建可觀察對象

從提交事件創(chuàng)建可觀察對象

開心每一天1111 2021-06-04 18:20:01
免責(zé)聲明:我對 Angular 和 RXJS 都是超級新手。我有一個簡單的表單,我試圖從中創(chuàng)建一個可觀察的對象。這將查找submit事件并更新組件中的某些值。但是,我收到一個this._subscribe is not a function錯誤。<form (submit)='submitForm()'>  <button type='submit'>Submit</button></form>我的組件import { Component, OnInit } from '@angular/core';import { Observable } from 'rxjs';import UtilsHelperService from '../services/utils-helper.service';@Component({...stuffs...})export class HomeComponent implements OnInit {  formSubmit: Observable<any>;  counter = 0;   constructor() { }  ngOnInit() {    const form = document.getElementsByTagName('form')[0];    this.formSubmit = Observable.create(form, 'submit');  }  submitForm() {    this.formSubmit.subscribe(      UtilsHelperService.formSubmitObserver(this.counter));  }}還有我的utils-helper.service.ts助手班...import {Injectable} from '@angular/core';@Injectable({  providedIn: 'root'})export default class UtilsHelperService {  static formSubmitObserver(counter) {    return {      next: (value) => {        counter++;      },      error: err => console.log(err),      complete: () => console.log('complete')    }  }}我看到formSubmit觀察者創(chuàng)建得很好。我有一個UtilsHelperService.formSubmitObserver方法,它返回一個observer具有 3 個必要方法的對象。所以,我不確定是我做錯了 Angular 的東西(我猜不是)還是 RXjs 的東西。
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個贊

我做了兩件事來解決這個問題:

  1. 使用fromEvent而不是Observable.createsubmit事件創(chuàng)建可觀察對象。這樣訂閱就不會拋出錯誤(調(diào)查原因..)

  2. 由于服務(wù)是單例,因此無法從服務(wù)更新組件屬性。您必須使用 eventemitter 或使用 AngularJS 樣式點(diǎn)規(guī)則。對于這種情況,我在組件本身中添加了所有輔助邏輯。

ngOnInit() {

  const form = document.getElementsByTagName('form')[0];

  this.formSubmit = fromEvent(form, 'submit');

  this.formSubmit.subscribe((submitEvent) => {

    this.counter++;

    this.formSubmitted.emit(this.counter);

  })

}

有了這個,我可以submitForm從組件和模板中刪除方法,并從服務(wù)中刪除輔助方法。


查看完整回答
反對 回復(fù) 2021-06-18
  • 2 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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