使用webpack从angularjs模块加载特定指令

伊斯拉加卜

我目前正在重构我的angularjs应用程序,以使用webpack和lazyloading。

只想从angularjs模块中加载一个指令,而不是加载整个模块以防止在我的生成包中使用未使用的代码

进行较小的改动是否可能

就像是

import {MyDirective} from './my-module.js';
xdeepakv

考虑到您使用的是es6 / es2015语法(babel / ts-node)。您的模块必须具有导出方法,而不能像导入一样使用它。使用导出语法:

// My module 
export { cube, foo, graph };
// Importing
import {cube } from './mymodule'

https://developer.mozilla.org/zh-CN/docs/web/javascript/reference/statements/export

但是,虽然有这样的导出指令。您需要创建一个指令函数,并在模块中重用该函数并同时导出。参见下面的例子

// Directive function
angular.module([]).directive('myCustomer', MyCustomDirective);

function MyCustomDirective() {
  return {
    restrict: 'E',
    scope: {
      customerInfo: '=info'
    },
    template: '<template>something here</template>'
  };
}

export {MyCustomDirective}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章