typescript - Angular 2 错误

标签 typescript angular

我遇到了 Angular 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');
  }
}

奖励.服务:

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);
  }
}

最佳答案

我通过将以下内容添加到我的项目中来复制它:

export class Subclass extends Base {}
class Base {}

然后通过首先定义基类来解决错误:

class Base {}
export class Subclass extends Base {}

示例:http://plnkr.co/edit/ge5gfckgXMp5ylUhKOEn - 先移动要定义的基类,没有报错。似乎它可能是相关的 - 但我无法在多文件场景中重现。

关于typescript - Angular 2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928466/

相关文章:

javascript - 将 PWA 添加到 Angular 8 - 模块构建失败

带报告的 Angular 最佳静态代码分析工具

angular - Angular网站上的Webpack错误

javascript - 预期有一个符合条件的请求

javascript - Angular 2测试无法加载自定义包

typescript - 是否可以要求 Dexie 排除一个或多个要保存在 IndexedDB 中的表属性?

javascript - 将数据传递给动态创建的 Angular 模块和组件

angular - 类型 'subscribe' 上不存在属性 'OperatorFunction<unknown, any>'

angular - 使用静态数组作为 mat-table 的数据源

typescript - 如何检查枚举中是否存在给定的字符串键?