我有一个文件Services.js
,我正在尝试加载我的所有个人服务。这些文件以单例形式公开。
Services.js
var Services = { };
export default Services;
然后我希望在服务下嵌套Sample Service,所以我可以调用例如Services.Sample.Operation()
`SampleService.js'
import Services from './Services';
Services.Sample = {
Operation: function() {
alert('operation!')
}
};
export default Services.Sample;
然后,我尝试导入:
import Services from './services/Services';
import SampleService from './services/SampleService';
alert(Services); // yields '[object object]'
alert(SampleService); // yields '[object object]'
alert(Services.Sample); // yields 'undefined' <--- This is the one I actually want to use
我怎样才能获得它,所以我可以参考Services.Sample而不是TanService。如何使SampleService嵌套在Services?
下答案 0 :(得分:3)
由于您在Services.js
导入SampleService.js
,但Services
变量不是来自{{1}的'原始'Services
变量,因此您的方式不起作用}}
我要做的是这样的事情:
Services.js
:
SampleService.js
SampleService = {
Operation: function() {
alert('operation!')
}
};
export default SampleService;
:
Services.js
然后:
import SampleService from './SampleService';
var Services = { };
Services.Sample = SampleService;
export default Services;
对于我来说,这似乎对我的模块的基本(in)依赖性和一致性更有意义(定义{{1}}在import Services from './services/Services';
alert(Services);
alert(Services.Sample);
export default Services;
而不是Services
中可以做什么,{{1 }}可以独立于Services.js
,加载SampleService.js
的模块不应该依赖SampleService
以及以后可能会改变的模块,...)。