我正在使用Ionic2并收到此错误:
Runtime Error Cannot read property 'prototype' of undefined
阅读here和here,它表明它可能与声明子类和基类的顺序有关。我正在使用带有子类的基类。你在哪里改变它们的声明顺序,是在进口中吗?在什么文件?
有趣的是我的代码工作正常,并且突然开始出现此错误,即使没有我更改基类或子类。我确实对另一个文件进行了更改,我现在已将其恢复,但仍然会收到错误。
更多详情:
错误:
main.js:24046Uncaught TypeError: Cannot read property 'prototype' of undefined at __extends (main.js:24046) at job.ts:26 at Object.<anonymous> (job.ts:24) at __webpack_require__ (bootstrap cdd11c2…:19) at Object.<anonymous> (main.js:46012) at __webpack_require__ (bootstrap cdd11c2…:19) at Object.<anonymous> (personModel.ts:21) at __webpack_require__ (bootstrap cdd11c2…:19) at Object.<anonymous> (locationModel.ts:11)
代码:
job.ts
import { Component, ViewChild, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { NavController, NavParams, InfiniteScroll, Content, Platform, Events, AlertController, LoadingController, ViewController, PopoverController } from 'ionic-angular';
import { JobModel } from '../model/jobModel';
import { PersonModel } from '../model/personModel';
import { SubCategoryModel } from '../model/subCategoryModel';
import { LocationModel } from '../model/locationModel';
import { RatingModel } from '../model/ratingModel';
import { ReviewPage } from '../review/review';
import { ChatsPage } from '../chats/chats';
import { CategoryPage } from '../category/category';
import { PersonPage } from '../person/person';
import { MyCameraPage } from '../mycamera/mycamera';
import { JobService } from '../service/jobService';
import { PersonService } from '../service/personService';
import { RatingService } from '../service/ratingService';
import { UtilityService } from '../utils/utilityService';
import { SearchJobsParent } from '../searchjobs/searchjobsParent';
import { MapRangeService } from '../service/mapRangeService';
@Component({
selector: 'job',
templateUrl: 'job.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class JobPage extends SearchJobsParent {
@ViewChild(Content) content: Content;
.....
searchJobsParent.ts
...
@Component({
})
export class SearchJobsParent {
...
离子信息
Your system information:
Cordova CLI: You have been opted out of telemetry. To change this, run: cordova telemetry on.
6.4.0
Ionic Framework Version: 2.0.0-rc.4
Ionic CLI Version: 2.1.18
Ionic App Lib Version: 2.1.9
Ionic App Scripts Version: 1.0.0
ios-deploy version: Not installed
ios-sim version: Not installed
OS: macOS Sierra
Node Version: v6.9.4
Xcode version: Not installed
答案 0 :(得分:1)
我通过使用Composition而不是Inheritance来解决这个问题。
您可以在以下位置创建具有常用功能的类的实例:
<强> myClas.ts 强>
private someCommonClass: SomeCommonClass = null;
...
this.someCommonClass = new SomeCommonClass(platform, ref);
<强> someCommonClass.ts 强>
constructor(platform: Platform)
答案 1 :(得分:0)
第一条评论here让我思考正确并引导我找到解决方案。我的问题是我试图将一个类导入我的基类。导入的类没有按正确的顺序捆绑。通过在app.component.ts中切换我的import语句的顺序,我能够解决问题。
在app.component.ts中:
<强>错误强>
import { ListPage } from '../pages/list/list';
import { LoginPage } from "../pages/login/login";
这是错误的,因为ListPage扩展了导入LoginPage的AppPage(此处未列出)。
从右强>
import { LoginPage } from "../pages/login/login";
import { ListPage } from '../pages/list/list';
答案 2 :(得分:0)
发生此问题是因为您安装了Native插件,此插件从IonicNativePlugin扩展为示例(Base64ToGallery,TextToSpeech,...)。所以,这引起了冲突,我终止了这个延伸,这是工作......
按照示例执行TextToSpeech插件的步骤:
YourProjectFolder\node_modules\@ionic-native\text-to-speech
index.d.ts
去申报课程:
export declare class TextToSpeech extends IonicNativePlugin{
/**
* This function speaks
* @param options {string | TTSOptions} Text to speak or TTSOptions
* @return {Promise<any>} Returns a promise that resolves when the speaking finishes
*/
speak(options: string | TTSOptions): Promise<any>;
/**
* Stop any current TTS playback
* @return {Promise<any>}
*/
stop(): Promise<any>;
}
删除extends IonicNativePlugin
并保存。
就是这样!
答案 3 :(得分:0)
有人尝试提供具有正确顺序的参考文件,如下所示:
/// <reference path='SomeCommonClass.ts' />
/// <reference path='TheExtendingClass.ts' />