2016-04-29 90 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,但是因为这个代码被转发而不是使用本地类,所以你会得到一个更令人困惑的错误。 –