简体   繁体   English

单个模块中的Angular 1.x和es6多个常量

[英]Angular 1.x and es6 multiple constants in a singular module

So i've got the duty of converting a es5 to an es6 module. 因此,我有责任将es5转换为es6模块。

How does one export/import an angular constant module if there are more than one constant in an angular module for ES6? 如果ES6的角度模块中有多个常数,如何导出/导入角度常数模块?

Eg constants.js 例如constants.js

export default angular.module('myConstants', [])
  .constant('foo', {result: 'bar'})
  .constant('blah', {result: 'bar'});

service.js service.js

import foo from './constants';
import blah from './constants'; // Tried this. This won't work

Does this mean i have to split my constants into separate modules? 这是否意味着我必须将常量拆分为单独的模块? Or should i just convert them into ES6 constants using the const keyword? 还是我应该使用const关键字将它们转换为ES6常量?

Pps using webpack and the babel loader 使用Webpack和Babel加载器的Pps

Ps I have to use psuedo-code as i cannot paste work code in. 附言我必须使用伪代码,因为我不能粘贴工作代码。

Angular constant service is just a type of service which is available during config phase and is the suggested way to store module constants (hence the name). constant服务只是在配置阶段可用的一种服务,是建议的存储模块常量的方法(因此命名)。 It isn't a real constant and can be overwritten any time with $provide.constant . 它不是真正的常量,可以随时用$provide.constant覆盖。

Chained angular.module(...) exports Module object and doesn't make sense in non-Angular context. 链接的angular.module(...)导出Module对象 ,在非Angular上下文中没有意义。

Keep separate exports for Angular modules and ES6 modules. 为Angular模块和ES6模块保留单独的导出。 It is conventional to export Angular module name property , a string that can be imported and used in other modules semantically. 通常导出Angular模块name属性 ,该字符串可以在语义上导入并在其他模块中使用。

export const foo = ...;

export default angular.module('myConstants', []).constant('foo', foo).name;

import myConstantsMod, {foo} from './constants';

angular.module(..., [myConstantsMod])...

It depends on what foo import is for, but generally non-default ES6 imports/ exports should be limited to language-specific usage (abstract classes, base objects, etc.) and shouldn't replace Angular modular functionality, which is indispensable due to its testability. 这取决于foo导入的用途,但通常非默认的ES6导入/导出应限于特定于语言的用法(抽象类,基础对象等),并且不应替换Angular模块化功能,这是由于以下原因必不可少的它的可测试性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM