TypeError:无法读取未定义的属性'$ http'-quasar

时间:2019-06-11 06:35:36

标签: vue.js axios quasar-framework

我正在互联网上寻找解决方案,但没有任何效果。有人知道我在做什么错吗?

我尝试导入index.js:

import Vue from 'vue'
import Vuex from 'vuex'
import Axios from '../boot/axios'
import axios from 'axios'
import VueRouter from 'vue-router'
Vue.use(axios)
Vue.use(VueRouter)
Vue.use(Axios)

axios.js:
import axios from 'axios'

export default async ({
  Vue
}) => {
  const token = localStorage.getItem('token')
  if (token) {
    Vue.prototype.$http.defaults.headers.common['Authorization'] = `Bearer ${token}` }

  axios.interceptors.response.use(function (response) {
    return response
  }, function (error) {
    if (error.response.status === 401) {
      // refreshtoken
    } else {
      return Promise.reject(error)
    }
  })
  Vue.prototype.$http = axios
}

TypeError:无法读取未定义的属性'$ http'

1 个答案:

答案 0 :(得分:1)

    import Vue from 'vue'
import Vuex from 'vuex'
import Axios from '../boot/axios'
import axios from 'axios'
import VueRouter from 'vue-router'
Vue.use(axios)
Vue.use(VueRouter)
Vue.use(Axios)

axios.js:
import axios from 'axios'
Vue.prototype.$http = axios

export default async ({
  Vue
}) => {
  const token = localStorage.getItem('token')
  if (token) {
    Vue.prototype.$http.defaults.headers.common['Authorization'] = `Bearer ${token}` }

  axios.interceptors.response.use(function (response) {
    return response
  }, function (error) {
    if (error.response.status === 401) {
      // refreshtoken
    } else {
      return Promise.reject(error)
    }
  })

}

在使用Vue.prototype。$ http

之前,应先定义Vue.prototype。$ http = axios。