【问题标题】:Not a Function Issue不是功能问题
【发布时间】: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


【解决方案1】:

我的猜测是,您可能在 WorkoutPlanService 中的某处序列化您的 WorkOutPlan 对象,然后将字符串解析回 WorkOutPlan 对象,这会导致您丢失关联的方法。

您是否随时对这些对象进行字符串化?

【讨论】:

  • 不,我没有对任何类型的对象进行字符串化。我只使用该服务来获取存储在服务中的 WorkoutList 对象列表。
猜你喜欢
  • 1970-01-01
  • 2020-09-12
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多