gpt4 book ai didi

angular - 为什么我在 Angular 6 中的服务出现错误?

转载 作者:行者123 更新时间:2023-12-04 02:57:47 25 4
gpt4 key购买 nike

我有一个服务:

import { EventEmitter, Injectable } from '@angular/core';

@Injectable()
export class PingOnActionListenerService {
removingListener = new EventEmitter();

removeListener(): void {
this.removingListener.emit();
}

在组件 A 我调用服务:

this._pingOnActionListenerService.removeListener();

在组件 B 我想监听服务:

    ngOnInit() {
this._pingOnActionListenerService.removingListener.subscribe(this.deactivatelistener());
}

deactivatelistener() {
window.removeEventListener('click', this.pingIfLastPingIsOld);
}

当我运行这段代码时,我在控制台收到错误消息:

core.js:1671 ERROR TypeError: generatorOrNext is not a function at SafeSubscriber.schedulerFn [as _next] (core.js:3565) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:195) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:133) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54) at EventEmitter.push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next (Subject.js:47) at EventEmitter.push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit (core.js:3537) at PingOnActionListenerService.push../src/app/core/services/ping-on-action/ping-on-action-listener.service.ts.PingOnActionListenerService.removeListener (ping-on-action-listener.service.ts:19) at CatchSubscriber.selector (logging-interceptor.ts:45) at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:33)

最佳答案

虽然与 OP 的问题没有直接关系,但我发布此内容以防对其他人有所帮助。

错误generatorOrNext is not a function,可能是由于执行以下类似的操作导致的,其中没有传递给.subscribe()的回调。

this.subject.valueChanges.subscribe(
// (newVal) => console.log(newVal)
);

关于angular - 为什么我在 Angular 6 中的服务出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52114584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com