简体   繁体   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?

I have AngularJS controller : 我有AngularJS controller

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

/* @ngInject */

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

Which is minified with gulp: 用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'));

And then I decided to migrate from plain Javascript to Typescript, starting with BaseController : 然后我决定从普通的JavaScript迁移到打字稿,从BaseController

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

After minification and concatenation, I got: 经过最小化和串联后,我得到:

Uncaught TypeError: Cannot read property 'prototype' of undefined Uncaught TypeError:无法读取未定义的属性“ prototype”

Related to line: 相关行:

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

Then I realised that Typescript compiler otputs BaseController as variable with IIFE: 然后,我意识到TypeScript编译器使用IIFE会将BaseController视为变量:

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

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

Problem IMO is related with variable/function hoisting in Javascript - when I manually replace variable and IIFE with function: 问题IMO与Java语言中的变量/函数提升有关-当我手动将变量和IIFE替换为函数时:

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

It works properly. 它正常工作。 Is there any idea to dispose of this problem, like forcing Typescript compiler to output function instead of variable with IIFE? 是否有解决此问题的想法,例如使用IIFE强制Typescript编译器输出函数而不是变量? Or, I can not change it, and I have to deal with it in other way? 或者,我无法更改它,而我必须以其他方式处理它? Thank you in advance for any help, I am pretty new to Typescript and I didn't realize that I can find problems like this. 在此先感谢您的帮助,我对Typescript还是陌生的,但我没有意识到我会发现这样的问题。

Likely, BaseController is not visible from ArticleController (because it is concatenated after for instance). 有可能的, BaseController是不是从可见ArticleController (因为它是为实例后连续)。

To avoid these sort of issues, write modular Typescript (es6 and/or commonjs style) and use webpack or browserify 为了避免此类问题,请编写模块化Typescript(es6和/或commonjs样式)并使用webpack或browserify

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

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