2016-04-29 107 views
3

我有一個角2錯誤,它似乎很難找到錯誤所在。錯誤在編譯期間未被檢測到,但瀏覽器在嘗試加載頁面時顯示該錯誤。角2錯誤

angular2-polyfills.js:349 Error: TypeError: Cannot read property 'prototype' of undefined 
     at __extends (http://localhost:3000/app/repository/award/award.service.js:7:72) 
     at eval (http://localhost:3000/app/repository/award/award.service.js:36:17) 
     at execute (http://localhost:3000/app/repository/award/award.service.js:47:14) 
    Error loading http://localhost:3000/app/main.js 

任何人都可以指向正確的方向嗎?

應用程序/ main.ts:

///<reference path="../../../node_modules/angular2/typings/browser.d.ts"/> 
import {bootstrap} from 'angular2/platform/browser'; 
import {HTTP_PROVIDERS} from 'angular2/http'; 
import {ROUTER_PROVIDERS} from 'angular2/router'; 
import {AppComponent} from './app.component'; 

bootstrap(AppComponent, [HTTP_PROVIDERS, ROUTER_PROVIDERS]) 
    .then(success => console.log(`Bootstrap success`)) 
    .catch(error => console.log(error)); 

庫:

import { Http, Response, Headers } from 'angular2/http'; 
import { Injectable } from 'angular2/core'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/concatMap'; 
import 'rxjs/add/operator/do'; 
import 'rxjs/add/operator/catch'; 
// import { CONFIG } from '../shared/config'; 
import { IEntity } from './ientity'; 

@Injectable() 
export abstract class Repository<T extends IEntity> 
{ 
    protected url = null; 

    constructor(protected _http: Http) {} 

    add(entity: T) { 
    let headers = new Headers(); 
    let body = { 
     name: entity.name 
    }; 

    headers.append('Authorization', 'Basic'); 

    return this._http 
     .post(`${this.url}`, JSON.stringify(body), { 
     headers: headers 
     }) 
     .map(res => res.json()) 
     .catch(this.handleError); 
    } 

    delete(entity: T) { 
    let headers = new Headers(); 
    headers.append('Authorization', 'Basic'); 

    return this._http 
     .delete(`${this.url}/${entity.id}`, { 
     headers: headers 
     }) 
     .catch(this.handleError); 
    } 

    list() { 
    let headers = new Headers(); 
    headers.append('Authorization', 'Basic'); 

    return this._http.get(`${this.url}`, { 
     headers: headers 
    }) 
     .map((response: Response) => 
     <T[]>response.json().data 
    ) 
     // .do(items => console.log(items)) 
     .catch(this.handleError); 
    } 

    get(id: number) { 
    let headers = new Headers(); 
    headers.append('Authorization', 'Basic'); 

    return this._http.get(`${this.url}/${id}`, { 
     headers: headers 
    }) 
     .map((response: Response) => <T>response.json()) 
     .catch(this.handleError); 
    } 


    update(entity: T) { 
    let headers = new Headers(); 

    let body = { 
     name: entity.name 
    }; 

    headers.append('Authorization', 'Basic'); 

    return this._http 
     .put(`${this.url}/${entity.id}`, JSON.stringify(body), { 
     headers: headers 
     }) 
     .map(res => res.json()) 
     // .do(res => console.log(body)) 
     .catch(this.handleError); 
    } 

    private handleError(error: Response) { 
    console.error(error); 
    return Observable.throw(error.json().error || 'Server error'); 
    } 
} 

award.service:

import { Http } from 'angular2/http'; 
import { Injectable } from 'angular2/core'; 
import { CONFIG } from '../shared/config'; 
import { Repository } from '../core'; 
import { IAward } from './iaward'; 

@Injectable() 
export class AwardService extends Repository<IAward> 
{ 
    protected url = CONFIG.baseUrls.awards; 

    constructor(protected _http: Http) { 
    super(_http); 
    } 
} 
+0

向我們展示您的應用/ main.ts –

+0

@DanielKucal請參閱上面 – Dblock247

+0

然後,讓我們從將'HTTP_PROVIDERS'放入引導數組中但不導入任何位置的事實開始。嘗試'從'angular2/http';'導入{HTTP_PROVIDERS}並讓我們知道你在哪裏。 –

回答

4

我通過添加以下到我的項目轉載此:

export class Subclass extends Base {} 
class Base {} 

然後工作圍繞誤差通過首先定義基類:

class Base {} 
export class Subclass extends Base {} 

實施例:http://plnkr.co/edit/ge5gfckgXMp5ylUhKOEn - 移動到首先被定義的基類,並且沒有錯誤。似乎它可能是相關的 - 但我無法在多個文件場景中重現。

+3

我相信這是由於這樣一個事實,即與函數不同,[JavaScript不會被懸掛](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes#Hoisting),所以當你試圖擴展'Base'時,那個類實際上還不存在。規範說這應該導致一個ReferenceError,但是因爲這個代碼被轉發而不是使用本地類,所以你會得到一個更令人困惑的錯誤。 –