vue操作全局变量和全局方法
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
// 存储数据
state: {
school:"卢本伟nb",
age:18,
gender:"man"
},
// 操作数据方法
mutations: {
daying(e,a){
this.state.school = "pipipi"
alert("hahaha" + a)
}
},
// 响应组件动作
actions: {
},
modules: {
}
})
调用:
<script> import { mapState, mapMutations } from "vuex"; export default { name: 'Test', data() { return { }; }, computed: { ...mapState(['school', 'age', 'gender']) }, methods: { ...mapMutations(['daying']) }, created() { } }; </script>
使用 ...mapMutations(['xxx']) 就能把对应全局方法获取进来
直接绑定事件使用即可。
<el-button type="primary" size="default" @click="daying"></el-button>