Ошибка типа: невозможно прочитать свойство '$ http' из неопределенного - квазар - PullRequest
0 голосов
/ 11 июня 2019

Я искал решение в интернете, но ничего не получалось.Кто-нибудь знает, что я делаю неправильно?

Я пытаюсь импортировать 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' из undefined

1 Ответ

1 голос
/ 11 июня 2019
    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 = axios перед использованием Vue.prototype. $ Http

...