vue.js - TypeError:无法读取未定义的属性“$http” - 类星体
问题描述
我在互联网上寻找解决方案,但没有任何效果。有谁知道我做错了什么?
我尝试导入 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”
解决方案
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
推荐阅读
- excel - VBA - Copy and Paste Multiple Times Between Excel Sheets
- python - 为 Keras 多输入模型发布 tf.data.Dataset
- javascript - React Router - Should I render all the routes at once?
- wordpress - WordPress 上的“请求未授权”Squareup 付款集成
- python - (discord.py) wait_for 函数不检查作者是否对消息做出反应
- r - 过滤功能从现有数据创建新的数据框
- reactjs - 使用 ${} reactjs 将我的 var 放入字符串参数时出错
- android - 使用 Agora 和 Vosk 时无法转录语音
- android - 在某些字符前省略三个点
- instagram - 如何从 Instagram API 获取媒体