如何在Vuex中访问vue实例 [英] How to access vue instance in Vuex

查看:74
本文介绍了如何在Vuex中访问vue实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Vue.js 项目的 main.js 中声明了一个全局变量.

I declare a global variable in the main.js of the Vue.js project.

Vue.prototype.$API = "myapihere"

我想从任何地方使用它.并且通过使用 this.$API 可以正常工作.

And I want to use this from everywhere. and it's work properly by using this.$API.

但在 Vuex 中它不起作用.

But in Vuex it does not work.

console.log(this.$API);

这里this.$API未定义.

我如何在 Vuex 中使用我的 $API.

How I use my $API in Vuex.

推荐答案

Vue 2 和 Vuex 3 答案

Vue 2 and Vuex 3 answer

在商店中可以通过访问this._vm

const store = new Vuex.Store({
  mutations: {
    test(state) {
      console.log(this._vm);
    }
  }
});

这篇关于如何在Vuex中访问vue实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆