无法静态分析'require(...,...)'

时间:2017-09-03 21:34:24

标签: javascript angularjs requirejs require

Angular 1.5.11

我将应用程序从RequireJS 2.2迁移到Webpack 3.5.5 + ES6模块。在创建包期间出现错误:

ERROR in ./app/services/routeResolver.js
Cannot statically analyse 'require(..., ...)' in line 175
 @ ./app/app.module.js 33:0-34
 @ ./app/app.js

我在这个routeResolver服务中有以下功能:

        ...
        var resolveDependencies = function ($q, $rootScope, dependencies) {
            var defer = $q.defer();
            var loader = new Loader();

            require(dependencies, function () {
                defer.resolve();
                $rootScope.$apply()
            });

            return defer.promise;
        }; 
        ...

dependencies参数是一个包含控制器路径的数组,例如app/controllers/nameOfController.js。该函数异步解析控制器。

如何用ES6语法替换此require?或者可能有一个外部模块来做它?

app.js

import './app.module';
import './app.config';
import './app.run';
import './controllers/firstController'
import './directives/firstDirective'
...
angular.bootstrap(document, ['myApp']);

app.module.js

import 'angular';
...
import './services/routeResolver';
...
var app = angular.module('myApp', [
  'routeResolver',
  ...
]);
export default app;

应用/服务/ routeResolver.js

var routeResolver = function () {
...
};
var moduleName = 'routeResolver';
angular.module(moduleName, []).provider(moduleName, routeResolver);
export default moduleName;

应用/控制器/ firstController.js

var injectParams = ['$scope', 'myEvents'];
var FirstController = function ($scope, myEvents) {
...
};
FirstController.$inject = injectParams;
var moduleName = 'firstController';
angular.module(moduleName, []).provider(moduleName, FirstController);
export default moduleName;

0 个答案:

没有答案