a页面存储 b页面读取
store-index.js
mport Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
// 初始化数据,等待获取
username: ''
}
const mutations = {
usernamelist(state,data) {
state.username = data
},
}
export default {
state,
mutations
}
a.vue
this.$store.commit("usernamelist", res.data.nickName);
//用于像store存储数据 方法名,数据 (方法名要和store中的方法名一致)
b.vue
this.$store.state.index.username
//用于读取store中的数据
本文介绍如何使用Vue.js结合Vuex进行应用状态管理。通过具体示例展示如何在a.vue页面存储数据到Vuex,并在b.vue页面读取这些数据。此方法有助于构建大型且维护良好的前端应用。
1248

被折叠的 条评论
为什么被折叠?



