<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Vue.js 和 Vuex 实现简单的计数器</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>
Vue.js 和 Vuex 实现简单的计数器

原文地址: https://www.cveoy.top/t/topic/n8fF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录