2017-01-23 48 views
3

我使用Ionic2和正在此錯誤性質「原型」:Ionic2:運行時錯誤無法讀取的不確定

Runtime Error 
Cannot read property 'prototype' of undefined 

閱讀herehere,這表明它可能與的順序子類和基類聲明。我正在使用一個帶有子類的基類。你在哪裏改變申報的順序,是否在進口?在什麼文件中?

有趣的是我的代碼工作正常,並且突然開始出現這個錯誤,即使沒有我改變基類或子類。我確實對另一個文件進行了修改,現在我已經恢復,但仍然出現錯誤。

更多細節:

錯誤:

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

如果我擴展基類,那麼我得到的錯誤。如果我不擴展它,那麼錯誤就會消失。那麼你應該如何擴展對象呢? – Richard

回答

1

我使用組成,而不是繼承解決了這個。

你創建你有共同的功能的類的實例:

myClas.ts

private someCommonClass: SomeCommonClass = null; 
    ... 
    this.someCommonClass = new SomeCommonClass(platform, ref); 

someCommonClass.ts

constructor(platform: Platform) 
+1

我認爲,這只是一個解決方法,但我們應該找到在打字稿中使用抽象的方法...... – Osify

+0

我有同樣的問題,但我不會罰款使用打字稿抽象的方式。有什麼想法嗎? – Mankeomorakort

+0

你能告訴我你是如何解決這個問題的?可能是小代碼片段? – Sampath

0

的第一個評論here讓我想到了,並讓我找到了解決方案。我的問題是我試圖將一個類導入到我的基類中。導入的類沒有按照正確的順序捆綁在一起。通過在app.component.ts中切換我的導入語句的順序,我能夠解決問題。

在app.component.ts:

錯誤

import { ListPage } from '../pages/list/list'; 
import { LoginPage } from "../pages/login/login"; 

這是錯誤的,因爲末頁擴展AppPage(此處未列出),其進口LoginPage。

import { LoginPage } from "../pages/login/login";  
import { ListPage } from '../pages/list/list'; 
0

這個問題發生,因爲你本機安裝插件,這個插件從IonicNativePlugin延伸例(Base64ToGallery,文字轉語音,...)。所以,這引起了衝突,這Iremoved延伸,它是工作...

根據這一步驟,例如用於文字轉語音插件:

  1. 在這個路徑YourProjectFolder\node_modules\@ionic-native\text-to-speech
  2. 打開文件index.d.ts
  3. 去申報類:

    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>; 
    } 
    
  4. 刪除extends IonicNativePlugin並保存。

就這樣!