使用narrowcript minification编写的角度提供程序是安全的

mas*_*plo 9 angularjs typescript

我已经编写了一个角度提供程序,需要在$ get函数中注入$ inject服务,但我不知道如何在打字稿中写这个并使其缩小安全性.像static $ injector = ['']; 适用于服务和控制器的符号.

打字稿:

export class ApiProvider implements IApiProvider {
    private baseRoute: string = '';
    private endpoints: { [name: string]: IApiEndPointConfig };

    static $inject = ['$injector']; //THIS DOES NOT WORK FOR PROVIDERS
    constructor() {
        this.baseRoute = '';
        this.endpoints = {};
    }

            // MORE CODE

    $get = ($injector: ng.auto.IInjectorService): { [name: string]: ApiEndpoint } => {
        var api: { [name: string]: ApiEndpoint } = {};
        var self:ApiProvider = this;
        angular.forEach(this.endpoints, (endpointConfig, name) => {
            api[name] = $injector.instantiate(ApiEndpoint, {
                baseRoute: self.baseRoute,
                endpointConfig: endpointConfig
            });
        });

        return api;
    };
}
Run Code Online (Sandbox Code Playgroud)

生成的$ get函数的javascript是:

this.$get = function ($injector) {
    var api = {};
    var self = _this;
    angular.forEach(_this.endpoints, function (endpointConfig, name) {
        api[name] = $injector.instantiate(Model.ApiEndpoint, {
            baseRoute: self.baseRoute,
            endpointConfig: endpointConfig
        });
    });

    return api;
};
Run Code Online (Sandbox Code Playgroud)

而我希望它是这样的:

this.$get = ['$injector', function ($injector) {
    var api = {};
    var self = _this;
    angular.forEach(_this.endpoints, function (endpointConfig, name) {
        api[name] = $injector.instantiate(Model.ApiEndpoint, {
            baseRoute: self.baseRoute,
            endpointConfig: endpointConfig
        });
    });

    return api;
}];
Run Code Online (Sandbox Code Playgroud)

mas*_*plo 16

我想我明白了.在更仔细地阅读$ injector文档之后,我发现$ inject字符串数组表示法可用于任何函数.因此,我做了类似的事情:

constructor(){
   this.$get.$inject = ['$injector'];
}

$get = ($injector: ng.auto.IInjectorService): { [name: string]: ApiEndpoint } => {
    //using $injector
};
Run Code Online (Sandbox Code Playgroud)

产生:

this.$get = function ($injector) {
   //using $injector                    
};

this.$get.$inject = ['$injector'];
Run Code Online (Sandbox Code Playgroud)

  • +1这是执行此操作的正确方法以及定义中存在https://github.com/borisyankov/DefinitelyTyped/blob/master/angularjs/angular.d.ts#L11-L14的原因;) (5认同)
  • 谈论访问"来源".非常感谢你的Typescript + Angular贡献(我见过你回答几乎所有相关的问题).youtube上也有很好的打字稿视频. (3认同)