Sample URLs:
/users/api/getUser
/users/api/addUser
/users/api/deleteUser/{userId}
我希望将公共路径(/ users / api)存储在全局变量中,并将其传递给每个服务,以便在调用REST端点之前将其与Request URL连接。我创建了一个类并存储了url前缀。但我无法在服务中获得它的价值。以下是代码段。
import CommonVariables from './commonVariables';
class UserService {
/*@ngInject*/
constructor($rootScope, Rest){
Object.assign(this, {$rootScope, Rest});
}
getUsers() {
console.log("commonVariables.getBaseURL: ", CommonVariables.baseURL);
console.log("commonVariables.getBaseURL: ", CommonVariables.getBaseURL());
return this.Rest.one("/users/api/getUser").get();
}
addUserDetails(request) {
return this.Rest.one("/users/api/addUser").customPOST(request);
}
}
export default UserService
CommonVariable
class CommonVariables {
/*@ngInject*/
constructor($rootScope){
Object.assign(this, {$rootScope});
let vm = this;
vm.baseURL = "/users/api";
}
getBaseURL() {
console.log("getBaseURL called");
return "/users/api";
}
}
export default CommonVariables
答案 0 :(得分:0)
将export default CommonVariables
更改为export default new CommonVariables();