我在angularjs 1.x应用程序中使用常见的js模块系统,并且在导入/导出常量时遇到困难。这就是我所拥有的。
index.js
'use strict';
var angular = require('angular');
module.exports = angular.module('constants', [])
.constant('constant1', require('./constant1'))
.constant('constant2', require('./constant2'))
.constant('constant3', require('./constant3'))
constant1.js
'use strict';
var SAMPLE1 = { 'TEST1' : 100 , 'Test2': 200 }
var SAMPLE2 = 300;
module.exports = SAMPLE1 ;
module.exports = SAMPLE2 ;
constant2.js
'use strict';
var SAMPLE3 = { 'TEST3' : 400 , 'Test4': 500 }
var SAMPLE4 = 600;
module.exports = SAMPLE3 ;
module.exports = SAMPLE4 ;
constant3.js
'use strict';
var SAMPLE5 = { 'TEST5' : 700 , 'Test6': 800 }
var SAMPLE6 = 900;
module.exports = SAMPLE5 ;
module.exports = SAMPLE6 ;
app.module.js
'use strict';
var angular = require('angular');
var myApp = angular.module('myApp', [
require('./common/services').name,
require('./components/constants').name
]);
运行应用程序时出现此错误
错误:$ injector:unpr
未知提供者:KEYProvider< - SAMPLE1< - MyService
答案 0 :(得分:2)
它表示您在声明注册服务时遇到错误MyService
。如果要导出多个变量,则必须编写module.exports.var1 = var1; module.exports.var2 = var2
(或exports.var1 = var1; exports.var2 = var2
)而不只是module.exports = var1; module.exports = var2,
,因为第二个赋值只会覆盖第一个。稍后您使用var1 = require('./file').var1
导入var1,使用var2 = require('./file').var2
导入var2