vuex 获取数据
npm i vuex --save
2: store文件夹下 index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const store = new Vuex.Store({
state: {
login: 'login',
test: 'test'
}
})
3: main.js
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import {store} from './store'
Vue.use(Vuex)
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
获取vuex中的数据一般写在computed(计算)中
import {mapState, mapGetters, mapMutations} from 'vuex'
export default {
data () {
return {
}
},
computed: {
...mapState(['user']),
...mapGetters(['person'])
},
methods: {
// ...mapMutations(['setUser']), //调用mutations中方法的第一种方式
submitUserNameForm () {
// this.setUser('456')
this.$store.commit('setUser','456') //调用mutations中方法的第二种方式
}
}
}
详情参看https://blog.csdn.net/a3060858469/article/details/80717824