免責聲明:我對 Angular 和 RXJS 都是超級新手。我有一個簡單的表單,我試圖從中創建一個可觀察的對象。這將查找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觀察者創建得很好。我有一個UtilsHelperService.formSubmitObserver方法,它返回一個observer具有 3 個必要方法的對象。所以,我不確定是我做錯了 Angular 的東西(我猜不是)還是 RXjs 的東西。
從提交事件創建可觀察對象
開心每一天1111
2021-06-04 18:20:01
