【发布时间】:2018-10-06 09:09:45
【问题描述】:
我在 Ionic Framework 上遇到了一个经典的“Not a Function”问题,希望您能帮助我更好地理解这个案例。
我正在尝试从数组中取出一个对象。该对象具有我使用简单的 getter 方法检索的私有属性。
上面整个类的这行代码执行的时候问题就出来了:
this.selectedWorkoutPlan = this.workoutPlanList.find(object => object.getId() === this.id);
控制台返回之前提到的错误。
这是我的 .ts 文件:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {WorkoutPlanService} from "../../services/workout-plan.service";
import {WorkoutPlan} from "../../models/workout-plan";
@IonicPage()
@Component({
selector: 'page-workout-plan-detail',
templateUrl: 'workout-plan-detail.html',
})
export class WorkoutPlanDetailPage {
id: number = 0;
title: string = "";
startDate: Date = new Date();
endDate: Date = new Date();
workoutPlanList: WorkoutPlan[] = [];
selectedWorkoutPlan: WorkoutPlan = new WorkoutPlan();
constructor(public navCtrl: NavController, public navParams: NavParams,
private workoutPlanService: WorkoutPlanService) {
}
ionViewDidLoad() {
this.id = this.navParams.get("id");
this.workoutPlanList = this.workoutPlanService.getWorkoutPlanList();
this.selectedWorkoutPlan = this.workoutPlanList.find(object => object.getId() === this.id);
}
}
这是 .ts 模型的类。
import {WorkoutExercise} from "./workout-exercise";
export class WorkoutPlan {
private title: string;
private exercises: WorkoutExercise[];
private startDate: Date;
private endDate: Date;
private id: number;
constructor() {
this.id = new Date().getTime();
}
setTitle(newTitle: string) {
this.title = newTitle;
}
setExercises(newExercises: WorkoutExercise[]) {
this.exercises = newExercises;
}
setStartDate(newStartDate: Date) {
this.startDate = new Date(newStartDate);
}
setEndDate(newEndDate: Date) {
this.endDate = new Date(newEndDate);
}
setId(newId: number) {
this.id = newId;
}
getTitle() {
return this.title;
}
getExercises() {
return this.exercises;
}
getStartDate() {
return this.startDate;
}
getEndDate() {
return this.endDate;
}
getId() {
return this.id;
}
}
我在语法中遗漏了什么吗?
在我看来,我认为数组的对象应该同时具有属性和方法,不是吗?
提前感谢您的宝贵时间。
【问题讨论】:
-
请另外提供您的 WorkoutPlanService。
-
如果您准确地发布错误的样子也会有所帮助
-
错误提示“object.getId() 不是函数”。
标签: javascript arrays function typescript ionic-framework