1.在pages目录同级目录,也就是根目录下创建store目录
2.store文件夹中新增一个index.js
敲入一些学来的代码
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
userInfo: "",
token: "",
auth:"",
},
mutations: {
SET_USERINFO(state, userInfo) {
state.userInfo = userInfo
},
SET_TOKEN(state, token) {
state.token = token
},
SET_AUTH(state, auth) {
state.auth = auth
}
}
})
export default store
new Vue({
store,
render: h => h(App)
}).$mount('#app')
然后运行,发现问题来了,报错,在根目录app.vue中
export default {
data() {
return {
state: null,
};
},
async created() {
try {
// 假设 getData 是一个异步操作,用于获取数据
const data = await this.getData();
// 在异步操作完成后,将获取到的数据赋值给组件实例的 'state' 属性
this.state = data;
} catch (error) {
console.error("Error in created hook:", error);
}
},
methods: {
async getData() {
// 这里可以实现获取数据的逻辑,例如通过 API 请求等
// 返回获取到的数据
},
},
};
这样,问题就解决了