代码之家  ›  专栏  ›  技术社区  ›  Patrick

根据需要忽略导入。将旧的AngularJS迁移到TypeScript

  •  0
  • Patrick  · 技术社区  · 7 年前

    用于加载我使用require的需求。js。因为我想与非需求兼容。我正在使用的js脚本 umd module

    例如,我有这个文件。

    自定义模块。输电系统

    import angular = require('angular');
    
    angular.module('customModule', []);
    

    import angular = require('angular');
    import customModule = require('customModule');
    
    angular
        .module('app')
        .factory('customFactory', ['customModule', class {
            constructor(
                private customModule
            ) { }
    
            out() {
                return this.customModule;
            };
        }]);
    

    自定义模块。js公司

    (function (factory) {
        if (typeof module === "object" && typeof module.exports === "object") {
            var v = factory(require, exports);
            if (v !== undefined) module.exports = v;
        }
        else if (typeof define === "function" && define.amd) {
            define(["require", "exports", "angular"], factory);
        }
    })(function (require, exports) {
        "use strict";
        Object.defineProperty(exports, "__esModule", { value: true });
        var angular = require("angular");
        angular.module('customModule', []);
    });
    

    主要的输电系统

    (function (factory) {
        if (typeof module === "object" && typeof module.exports === "object") {
            var v = factory(require, exports);
            if (v !== undefined) module.exports = v;
        }
        else if (typeof define === "function" && define.amd) {
            define(["require", "exports", "angular"], factory);
        }
    })(function (require, exports) {
        "use strict";
        Object.defineProperty(exports, "__esModule", { value: true });
        var angular = require("angular");
        angular
            .module('app')
            .factory('customFactory', ['customModule', (function () {
                function class_1(customModule) {
                    this.customModule = customModule;
                }
                class_1.prototype.out = function () {
                    return this.customModule;
                };
                ;
                return class_1;
            }())]);
    });
    

    main.js 有一个 define(["require", "exports", "angular"], factory); . 但我想 define(["require", "exports", "angular", "customModule"], factory); .

    如果我更换 return this.customModule; main.ts 具有 return customModule;

    Changes of main.js after removing constructor

    如果这个问题得到解决,我可以将我的模块与TypeScript一起使用,就像我与JavaScript一起使用一样<3.

    1 回复  |  直到 7 年前
        1
  •  1
  •   Fenton    7 年前

    TypeScript编译器有一个优化,所以如果你有下面的代码,神奇的事情就会发生。。。

    import * as angular from 'angular';
    import * as customModule from 'customModule';
    
    angular
        .module('app')
        .factory('customFactory', ['customModule', class {
            constructor(
                private customModule
            ) { }
    
            out() {
                return this.customModule;
            };
        }]);
    

    customModule ,它优化了导入。

    您可以通过使用导入的模块或使用强制导入进行修复,如下所示:

    import * as angular from 'angular';
    import 'customModule'; // <-- FORCED
    
    angular
        .module('app')
        .factory('customFactory', ['customModule', class {
            constructor(
                private customModule
            ) { }
    
            out() {
                return this.customModule;
            };
        }]);