繁体   English   中英

AngularJS:TypeScript编译过程和gulp-uglify-是否可以通过IIFE强制TS生成函数而不是变量?

[英]AngularJS: TypeScript compile process and gulp-uglify - is there a way to force TS to produce functions instead of variable with IIFE?

我有AngularJS controller

ArticleController.prototype = Object.create(BaseController.prototype);

/* @ngInject */

function ArticleController (CommunicationService){
    //Some code not related with problem
}

用gulp最小化:

return gulp.src(pathsToMinify)
        .pipe(require('gulp-ng-annotate')())
        .pipe(require('gulp-uglify')())
        .pipe(require('gulp-concat')('application.min.js'))
        .pipe(gulp.dest('dist'));

然后我决定从普通的JavaScript迁移到打字稿,从BaseController

class BaseController {
    constructor() {
        //Some code not related with problem
    }
}

经过最小化和串联后,我得到:

Uncaught TypeError:无法读取未定义的属性“ prototype”

相关行:

ArticleController.prototype = Object.create(BaseController.prototype);

然后,我意识到TypeScript编译器使用IIFE会将BaseController视为变量:

var BaseController = (function () {
    function BaseController() {

    }
    BaseController.prototype.setPath = function (path) {
        this._path = path;
    };
    //Some code not related with problem
    return BaseController;
})();

问题IMO与Java语言中的变量/函数提升有关-当我手动将变量和IIFE替换为函数时:

function BaseController() {
}
//Some code not related with problem

它正常工作。 是否有解决此问题的想法,例如使用IIFE强制Typescript编译器输出函数而不是变量? 或者,我无法更改它,而我必须以其他方式处理它? 在此先感谢您的帮助,我对Typescript还是陌生的,但我没有意识到我会发现这样的问题。

有可能的, BaseController是不是从可见ArticleController (因为它是为实例后连续)。

为了避免此类问题,请编写模块化Typescript(es6和/或commonjs样式)并使用webpack或browserify

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM