我目前在项目中使用 angular 1.5,我的路由如下所示:
const routes = ($stateProvider) => {
$stateProvider
.state('update', {
url: '/update',
template: '<update></update>',
resolve: {
lazyload: [ '$q', '$ocLazyLoad', function ($q, $ocLazyLoad) {
console.log('start lazy');
let deferred = $q.defer();
require.ensure([], function () {
let module = require('../components/update/update.module');
let module = require('../components/update/update.module');
$ocLazyLoad.load({
name: module.default.name
});
deferred.resolve(module);
});
return deferred.promise;
}
]
}
})
.state('login', {
url: '/login',
template: '<login></login>',
resolve: {
lazyload: [ '$q', '$ocLazyLoad', function ($q, $ocLazyLoad) {
let deferred = $q.defer();
require.ensure([], function () {
let module = require('../components/login/login.module');
$ocLazyLoad.load({
name: module.default.name
});
deferred.resolve(module)
});
return deferred.promise;
}
]
}
});
};
当我尝试在这样的单独函数中提取重复代码时:
function load (modulePath) {
return function ($q, $ocLazyLoad) {
let deferred = $q.defer();
require.ensure([], function () {
let module = require(modulePath);
$ocLazyLoad.load({
name: module.default.name
});
deferred.resolve(module);
});
return deferred.promise;
}
}
并以这种方式使用加载功能:
$stateProvider
.state('update', {
url: '/update',
template: '<update></update>',
resolve: {
lazyload: [ '$q', '$ocLazyLoad', load('../components/update/update.module')]
}
})
路由不再起作用,我在 webpackMissingModule 处收到此错误“找不到模块”。