我有一个文件,Services.js
试图将所有单独的服务加载到其中。这些文件显示为单例。
Services.js
var Services = { };
export default Services;
然后,我想将Sample Service嵌套在Services下,因此可以调用例如 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而不是tan SampleService。如何使SampleService嵌套在服务下?
你的方式不起作用,因为您要导入Services.js
的SampleService.js
,但Services
变量不是“原始”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;
接着:
import Services from './services/Services';
alert(Services);
alert(Services.Sample);
export default Services;
但这似乎也更有意义,我就基本(中)的依赖和你的模块(定义什么的一致性,Services
可以做到在Services.js
不SampleService.js
,SampleService
可能是独立的Services
,模块加载Services.js
不应该依赖SampleService.js
以及可能以后更改。 ..)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句