!DOCTYPE htmlhtml head meta charset=utf-8 titletitle script src=vuejsscript script src=vuexjsscript head body div id=app div class=count button click=calc计数button spanthis$storestatecount
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/vuex"></script>
</head>
<body>
<div id="app">
<div class="count">
<button @click="calc">计数</button>
<span>{{ $store.state.count }}</span>
</div>
</div>
<script>
const store = new Vuex.Store({
state: { count: 0 },
mutations: {
increment (state) {
state.count++
}
},
actions: {
add (context) {
context.commit('increment')
}
}
})
var vm = new Vue({
el: '#app',
store,
methods: {
calc () {
this.$store.dispatch('add')
}
}
})
</script>
</body>
</html
原文地址: https://www.cveoy.top/t/topic/fgfd 著作权归作者所有。请勿转载和采集!