服务类继承和文件顺序

时间:2016-01-03 15:09:11

标签: angularjs typescript

我有服务(TypeScript中的类)app/resources/GeneralResource.ts以及文件夹app/resources/中的其他服务。我想从GeneralResources继承我的所有资源。

按字母顺序排在GeneralResource.ts个文件之后的所有文件(类)都正常工作。但是GeneralResource.ts之前的所有文件(类)都有执行时错误。

以下是一个示例类:

export class DefectReasonResource extends GeneralResource implements IDefectReasonResource {

    static $inject = ['$q', '$http', 'baseUrl'];

    constructor(public $q:ng.IQService, public $http:ng.IHttpService, public baseUrl:string) {
       super($q, $http);
    }

    // ...

这个类编译正常:

var app;
(function (app) {
    var common;
    (function (common) {
        var DefectReasonResource = (function (_super) {
            __extends(DefectReasonResource, _super);
            function DefectReasonResource($q, $http, baseUrl) {
                _super.call(this, $q, $http);
                this.$q = $q;
                this.$http = $http;
                this.baseUrl = baseUrl;
            }
            DefectReasonResource.$inject = ['$q', '$http', 'baseUrl'];
            return DefectReasonResource;
        })(common.GeneralResource);
        common.DefectReasonResource = DefectReasonResource;
        angular.module('myApp').service('defectReasonResource', DefectReasonResource);
    })(common = app.common || (app.common = {}));
})(app || (app = {}));

但是当我打开文件时,我在浏览器中出错:

  
    

未捕获的TypeError:无法读取未定义的属性'prototype'

  

我只想强调我对GeneralResource之后声明的所有类都没有这个问题。

我认为删除继承并将GeneralResource注入我的所有服务会更好。但我想知道可以修复继承解决方案吗?

1 个答案:

答案 0 :(得分:1)

您没有详细说明您的设置是什么样的。但是当我使用(TypeScript)内部模块时遇到了这个问题。您只需要确保不要忘记添加引用注释:

///<reference path="relative/path/to/GeneralResource.ts" />

app/resources/serviceName.ts个文件中。注释的目的是确保最终app.js文件中的文件(或其内容)顺序正确(如果使用--outFile标志)。