<!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
!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

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

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