Ionic2:运行时错误无法读取属性';原型&x27;未定义的
我使用的是Ionic2,出现以下错误: 阅读和分析,它表明它可能与子类和基类的声明顺序有关。我将基类与子类一起使用。您在哪里更改它们的申报顺序,是在进口中吗?在什么档案里 有趣的是,我的代码工作得很好,突然出现了这个错误,即使我没有更改基类或子类。我确实对另一个文件进行了更改,现在已恢复,但仍然出现错误 更多详情: 错误: searchJobsParent.tsIonic2:运行时错误无法读取属性';原型&x27;未定义的,ionic2,Ionic2,我使用的是Ionic2,出现以下错误: 阅读和分析,它表明它可能与子类和基类的声明顺序有关。我将基类与子类一起使用。您在哪里更改它们的申报顺序,是在进口中吗?在什么档案里 有趣的是,我的代码工作得很好,突然出现了这个错误,即使我没有更改基类或子类。我确实对另一个文件进行了更改,现在已恢复,但仍然出现错误 更多详情: 错误: searchJobsParent.ts import { Component, ViewChild, ChangeDetectionStrategy, ChangeDetec
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;
.....
...
@Component({
})
export class SearchJobsParent {
...
private someCommonClass: SomeCommonClass = null;
...
this.someCommonClass = new SomeCommonClass(platform, ref);
constructor(platform: Platform)
离子信息
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
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;
.....
...
@Component({
})
export class SearchJobsParent {
...
private someCommonClass: SomeCommonClass = null;
...
this.someCommonClass = new SomeCommonClass(platform, ref);
constructor(platform: Platform)
someCommonClass.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;
.....
...
@Component({
})
export class SearchJobsParent {
...
private someCommonClass: SomeCommonClass = null;
...
this.someCommonClass = new SomeCommonClass(platform, ref);
constructor(platform: Platform)
第一句话让我想到了正确的答案。我的问题是我试图将一个类导入基类。导入的类没有按正确的顺序绑定。通过切换app.component.ts中导入语句的顺序,我能够解决这个问题
在app.component.ts中:
错误
import { ListPage } from '../pages/list/list';
import { LoginPage } from "../pages/login/login";
这是错误的,因为ListPage扩展了AppPage(此处未列出),它导入了LoginPage
对
import { LoginPage } from "../pages/login/login";
import { ListPage } from '../pages/list/list';
发生此问题的原因是您安装了本机插件,并且此插件扩展自IonicNativePlugin(例如Base64ToGallery、TextToSpeech等)。所以,这导致了冲突,我移动了这个扩展,这是工作 例如,按照以下步骤创建TextToSpeech插件:
YourProjectFolder\node\u 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>;
}
export declare class TextToSpeech扩展了IonicNativePlugin{
/**
*此函数说明
*@param options{string | TTSOptions}要讲话的文本或TTSOptions
*@return{Promise}返回一个承诺,该承诺在讲话结束时解析
*/
说话(选项:string | TTSOptions):承诺;
/**
*停止任何当前的TTS播放
*@return{Promise}
*/
停止:承诺;
}
扩展IonicNativePlugin
并保存就这些 是否有人试图提供一个顺序正确的参考文件,如下所示:
/// <reference path='SomeCommonClass.ts' />
/// <reference path='TheExtendingClass.ts' />
//
///
如果扩展基类,则会出现错误。如果我不扩展它,那么错误就会消失。那么你应该如何扩展对象呢?我想,这只是一个解决办法,但我们应该找到在typescript中使用抽象的方法…我有同样的问题,但我不喜欢使用typescript抽象的方法。你能告诉我你是如何解决这个问题的吗?可能是小代码段?不要使用扩展SomeClass
。而是导入SomeClass
并调用this.SomeClass.someFunction()
我需要将它注入子类的构造函数中吗?