簡體   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