就像在main.js中一样,我正在尝试从帮助函数文件访问我的商店:
import store from '../store'
let auth = store.getters.config.urls.auth
Run Code Online (Sandbox Code Playgroud)
但是它记录了一个错误:
未捕获的TypeError:无法读取未定义的属性"getters".
我试过了
this.$store.getters.config.urls.auth
Run Code Online (Sandbox Code Playgroud)
结果相同.
商店:
//Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
config: 'config',
},
getters: {
config: state => state.config
},
});
export default store
Run Code Online (Sandbox Code Playgroud)
如何在组件外部使用我的商店?
我需要全局变量来处理错误。但我不想为每个组件设置输入变量。如何在没有输入变量的情况下观察组件 ABC 中的 $errors?
(without <abc :errors="$errors"></abc>)
Run Code Online (Sandbox Code Playgroud)
索引.js:
Vue.prototype.$errors = {};
new Vue({
el: '#app',
render: h => h(App),
}
Run Code Online (Sandbox Code Playgroud)
应用程序.vue:
...
name: 'App',
components: {
ABC
}
...
methods:{
getContent() {
this.$errors = ...from axis...
}
Run Code Online (Sandbox Code Playgroud)
组件 ABC:
<template>
<div>{{ error }}</div>
</template>
...
watch: {
???
}
Run Code Online (Sandbox Code Playgroud)