<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Vuex 异步操作最佳实践</title>
		<script src="../../vue.js"></script>
		<script src="../../vuex.js"></script>
	</head>
	<body>
		<div id="app">
			<button @click="asyc">异步操作</button>
			<p>{{count}}</p>
		</div>
		<script>
			var store = new Vuex.Store({
				state: { count: 0 },
				mutations: {
					increment(state) {
						state.count++;
					}
				},
				actions: {
					asyc({ commit }) {
						setTimeout(function() {
							commit('increment');
						}, 1000);
					}
				}
			});
			var vm = new Vue({
				el: '#app',
				store,
				computed: {
					count() {
						return this.$store.state.count;
					}
				},
				methods: {
					asyc() {
						this.$store.dispatch('asyc');
					}
				}
			});
		</script>
	</body>
</html>
Vuex 异步操作最佳实践:使用 Actions 和 Mutations

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

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