我想了解在f.e中拥有多个服务工厂是否是一个好习惯。 home.services.js
文件,里面可能有其他一些服务。
home.services.js 如下所示:
(function () {
'use strict';
angular.module('fuelComparatorApp.homeServices', [])
.service('sayHelloService', sayHelloService)
.service('someService', someService);
sayHelloService.$inject = ['$http', '$q'];
someService.$inject = ['$http', '$q'];
function someService() {
const factory = {
someMessageObject: [],
someServiceMessage : someServiceMessage
};
function someServiceMessage() {
factory.someMessageObject = {
someMessageString: "Some message from someService.someServiceMessage"
};
}
return factory;
}
function sayHelloService() {
const factory = {
//properties
helloMessage: [],
sayHi: sayHi,
byeMessage: [],
sayBye: sayBye
};
function sayHi(){
factory.helloMessage = {
Message: "hello message from helloService!"
};
}
function sayBye(){
factory.byeMessage = {
Message: "bye message from helloService!"
};
}
return factory;
}
})();
这个结构好吗?或者它是否只允许构建一个/ *.service.js
工厂,其中可能包含多个服务/功能?
答案 0 :(得分:1)
将每个服务放在自己的文件中并将文件命名为与服务相同的文件要好得多。通过这种方式,您将能够使用" open resource"您喜欢的IDE /编辑器中的快捷方式,以查找服务的代码。当代码库变大时,这比文件中的文本搜索更实用。