VUEJs:main.js中的导入服务

时间:2018-02-26 00:08:28

标签: vuejs2 axios

我确实在vuejs

中创建了身份验证服务
import axios from "axios";

var authenticateService = {
  login: function(Username, Password) {
    var form = new FormData();
    form.append("username", Username);
    form.append("password", Password);
    return axios.post(LOGIN_API, form);  
  }
}

export default authenticateService;

我想在main.js中使用这个服务,我有一个axios拦截器。如果从服务器收到任何401响应,我有拦截器重新登录

import authenticateService from "./service/AuthenticationService";
import axios from "axios";

axios.interceptors.response.use(undefined, err => {

  let res = err.response;
  if (res.status === 401 && res.config && !res.config.__isRetryRequest) {
    return new Promise((resolve, reject) => {
      authenticateService//Error here. authenticateService undefined
      .login('user1username', 'user1password')
        .then(result => {
          localStorage.setItem("expire_time", response.data.expires_in);
          localStorage.setItem("access_token", response.data.access_token);
          err.config.__isRetryRequest = true;
          err.config.headers.Authorization =
            "Bearer " + response.data.access_token;
          resolve(err);
        })
        .catch(e => {
          localStorage.removeItem("expire_time");
          localStorage.removeItem("access_token");
          reject(e);
        });
    });
  }
});

此拦截器抛出错误说" authenticateService未定义"。虽然我已经导入了authenticateService,但我没有定义它。不知道我在这里做错了什么。

编辑从登录功能返回axios对象

0 个答案:

没有答案