我希望有人能在这里指出我正确的方向。 在我的 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 模块(在此示例中缩写)。该模块作为单独的模块导入到我的 store.ts 中。
export default {
state: {
.....
},
mutations: {
......
},
actions: {
testMyVar(context: any) {
console.log(this.$test); //What am I doing wrong here???
}
},
getters: {
....
}
}
编辑:基本上我想做的是模拟一个库并从本地开发站上的index.html启动它。然后从我的商店模块中提供启动的课程。
在生产中,类已经启动,变量只需要从我的 vuex 存储模块访问即可。
谢谢
import Vue from 'vue'
export default {
state: {
.....
},
mutations: {
......
},
actions: {
testMyVar(context: any) {
console.log(Vue.prototype.$test); //just import vue in store
}
},
getters: {
....
}
}
您还可以尝试以下操作:this.$store.state.varName