【发布时间】:2022-01-22 05:03:02
【问题描述】:
我希望有人可以在这里为我指明正确的方向。 在我的 Vue.js 应用程序中,我在 main.ts 中定义了一个全局变量 $test
import Vue from 'vue'
import App from './App.vue'
import { store } from '../src/store/store';
Vue.config.productionTip = false
Vue.prototype.$store = store;
Vue.prototype.$test = 'Testing';
new Vue({
store,
render: h => h(App)
}).$mount('#app');
如何从 Vuex 存储模块访问此变量?我试过this.$test 和其他组合都没有运气。
这是来自 vuex 存储模块(在此示例中缩写)。该模块作为单独的模块导入我的 store.ts。
export default {
state: {
.....
},
mutations: {
......
},
actions: {
testMyVar(context: any) {
console.log(this.$test); //What am I doing wrong here???
}
},
getters: {
....
}
}
编辑:基本上我想做的是模拟一个库并从我本地开发站上的 index.html 启动它。然后从我的商店模块中使启动的类可用。
在生产中,该类已经启动,只需要从我的 vuex 存储模块中访问该变量即可。
谢谢
【问题讨论】:
-
为什么不直接把变量添加到vuex状态,在需要的时候调用呢?
-
感谢您的意见。这就是解决方案!
标签: javascript vuejs2 vuex