以角度js 1.4在服务之间共享数据

时间:2018-03-26 07:07:17

标签: angularjs angularjs-service

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

1 个答案:

答案 0 :(得分:0)

export default CommonVariables更改为export default new CommonVariables();