【问题标题】:How use axios instance properties in vuex store?如何在 vuex store 中使用 axios 实例属性?
【发布时间】:2019-04-20 05:04:10
【问题描述】:

我在 main.js 中有 crate Axios 实例属性:

import Axios from "axios";

const axios = Axios.create({
  baseURL: process.env.VUE_APP_BASE_URL
})

Vue.prototype.$http = axios;

我可以在 Vue 实例中使用它,但我如何在 store.jsvuex 商店)中使用它?

this.$http 找不到。

TypeError: _this.$http is not a function

我得到了答案:

Use Vue.prototype.$http

【问题讨论】:

  • TypeError: 无法读取未定义的属性“$http”
  • 您的问题的最佳答案不是很清楚。如果您的问题已通过 Cloud 的答案解决,请在问题中删除您自己的答案,否则添加您自己的答案作为答案并接受它作为正确的解决方案。谢谢!

标签: vue.js vuex


【解决方案1】:

你可以导入Vue来使用它。

import Vue from 'vue'

async action() {
  const response = Vue.http.get('api/users');
}

【讨论】:

  • 我正在尝试在我的 store.js 中使用 axios 实例。我曾尝试使用 Vue.$myInstance 访问 axios 实例,但未定义。我也尝试过 this._vm.$myinstance 但 $myInstance 不是这个 ome 的承诺。我需要帮助
  • 您是否尝试在不使用 $ 符号的情况下访问它?
  • 是的,它返回 undefined
  • 步骤: 1. 在main.js/index.js, Vue.prototype.$http = axios; 2. 在store.js 中,使用``` import Vue from 'vue' Vue.http.get('api/users') 访问axios 实例; ```
猜你喜欢
  • 2020-03-25
  • 2022-01-19
  • 2020-05-10
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 2022-12-10
  • 2017-06-10
  • 1970-01-01
相关资源
最近更新 更多