【发布时间】: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