【问题标题】:How to fix Type Error not a function in angular如何修复类型错误不是角度的函数
【发布时间】:2019-10-07 03:36:50
【问题描述】:

让一个简单的网站选择您自己的冒险组件。每当我尝试调用我为代表故事事件而创建的类的函数时都会出现此错误。

export class ZombieCharacterOutcome {
    id: string;
    text: string;
    public characteristic: ZombiePlayerCharacteristic;
    events: string[];

    public getNextEvent(): string {
        var rand = Math.random() * this.events.length;
        return this.events[rand];
    }
}

我正在使用 firestore 数据库来存储数据,这也是初始化新结果的方式。

getCharaterOutcome(outcomeID: string): Observable<ZombieCharacterOutcome> 
{
    console.log(outcomeID);
    return this.db.doc('characterOutcomes/' + 
    outcomeID).snapshotChanges().pipe(map(choice => {
    const data = choice.payload.data() as ZombieCharacterOutcome;
    if (data) {
       data.id = choice.payload.id;
    }
    return data;
  }));
}

这是来自前端调用的 Angular 组件的代码...

nextEvent() {
    if(this.eventCount < this.maxEvent) {
        this.characterService.getCharacterEvent(this.currentOutcome.getNextEvent()).subscribe(event => {
            this.currentOutcome = null;
            this.currentEvent = event;
            this.loadChoices();
        });
    } else {
        this.currentEvent = null;
        this.currentChoices = [];
        this.currentOutcome = null;
    }
}

ERROR TypeError: this.currentOutcome.getNextEvent 不是函数 在 ZombieCharacterComponentComponent.push../src/app/zombie-character-component/zombie-character-component.component.ts.ZombieCharacterComponentComponent.nextEvent (zombie-character-component.component.ts:60) 在 Object.eval [as handleEvent] (ZombieCharacterComponentComponent.html:28) 在句柄事件(core.js:10251) 在 callWithDebugContext (core.js:11344) 在 Object.debugHandleEvent [as handleEvent] (core.js:11047) 在 dispatchEvent (core.js:7710) 在 core.js:8154 在 HTMLAnchorElement。 (平台-browser.js:988) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421) 在 Object.onInvokeTask (core.js:3811)

【问题讨论】:

  • 您好,请提供代码,您在其中实例化 ZombieCharacterOutcome 类。
  • 参考这个答案,它可能会解决你的问题:stackoverflow.com/a/51764292/10262090
  • 如果将 getNextEvent(): string 更改为 getNextEvent() 是否还会出现错误?
  • @DylanAnlezark 删除类型并没有影响错误
  • @Batajus 我已经包含了用于从我的 Firestore 数据库中检索的代码。调用这个函数来实例化新的 ZombieCharacterOutcomes

标签: angular


【解决方案1】:

ZombieCharacterOutcome 变量尚未实例化。

我在返回之前重新设计了我的 getCharacterOutcomeCode 以创建一个新的 ZombieCharacterOutcome,现在一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2019-11-22
    • 2018-12-03
    • 2022-01-21
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多