我正在使用Ionic2,但收到此错误:
Runtime Error Cannot read property 'prototype' of undefined
在这里和这里阅读,它表明它可能与声明子类和基类的顺序有关。 我正在使用带有子类的基类。您在哪里更改它们的声明顺序,是在进口中吗?在什么文件中?
有趣的是我的代码工作正常,突然开始出现此错误,即使我没有更改基类或子类。我确实对另一个文件进行了更改,现在我已经恢复了该文件,但仍然收到错误。
更多详情:
错误:
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)
法典:
工作.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;
.....
搜索工作父母.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
我通过使用组合而不是继承来解决这个问题。
创建具有常见功能的类的实例:
myClas.ts
private someCommonClass: SomeCommonClass = null;
...
this.someCommonClass = new SomeCommonClass(platform, ref);
someCommonClass.ts
constructor(platform: Platform)
这里的第一条评论让我思考正确,并引导我找到解决方案。我的问题是我试图将一个类导入我的基类。导入的类未按正确的顺序捆绑。通过在app.component.ts中切换导入语句的顺序,我能够解决问题。
在 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';
此问题是因为您安装了本机插件,并且此插件从IonicNativePlugin扩展而来,例如(Base64ToGallery,TextToSpeech,...)。所以,这引起了冲突,我删除了这个扩展,这是工作......
例如,按照以下步骤到文本到语音转换插件:
- 在此路径
YourProjectFoldernode_modules@ionic-nativetext-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
并保存。
仅此而已!
有没有人尝试提供具有正确顺序的参考文件,如下所示:
/// <reference path='SomeCommonClass.ts' />
/// <reference path='TheExtendingClass.ts' />