Vuex

本文详细介绍了如何在Vue项目中使用Vuex进行状态管理,包括安装配置、存取值操作及解决异步加载问题。通过具体示例展示了Vuex各模块的作用及使用步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

课程目标

一、了解vuex中的各个js文件的用途
二、利用vuex存值
三、利用vuex取值
四、Vuex的异步加载问题

一、了解vuex中的各个js文件的用途

变量传值的演变形式
在这里插入图片描述

图解vuex
在这里插入图片描述

使用步骤

  1. 安装
    npm install vuex -S

  2. 创建store模块,分别维护state/actions/mutations/getters
    在这里插入图片描述

  3. 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块

import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'
Vue.use(Vuex)
const store = new Vuex.Store({
 	state,
 	getters,
 	actions,
 	mutations
 })

 export default store
  1. 在main.js中导入并使用store实例
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
// process.env.MOCK && require('@/mock')
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui' 
import axios from '@/api/http'  
// import axios from 'axios' 
import VueAxios from 'vue-axios'

Vue.use(ElementUI)
Vue.use(VueAxios,axios)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  data(){
	  return{
		  Bus:new Vue({
		  	
		  })
	  }
  },
  router,
	store,
  components: { App },
  template: '<App/>'
})

二、利用vuex存、取值

VuexPage1.vue

<template>
	<div>
		<ul>
			<li>这是第一张页面:{{msg}}</li>
			<li>
				<input v-model="newName" />
				<button @click="buy">盘它</button>
			</li>
		</ul>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				newName:''
			};
		},
		computed:{
			msg(){
				//不推荐,因为vuex遵循了编码解耦的原则,各模块各司其职
				return this.$store.state.resturantName;
			}
		},
		methods:{
			buy(){
				console.log(this.newName);
				this.$store.commit('setResturantName',{
					resturantName:this.newName
				})
			}
		}
	}
</script>

<style>

</style>

VuexPage2.vue

<template>
	<div>
		<ul>
			<li>这是第二张页面:{{msg}}</li>
		</ul>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				
			};
		},
		computed:{
			msg(){
				return this.$store.getters.getResturantName;
			}
		}
	}
</script>

<style>

</style>

添加路由index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import AppMain from '@/components/AppMain'
import Articles from '@/views/sys/Articles'
import VuexPage1 from '@/views/sys/VuexPage1'
import VuexPage2 from '@/views/sys/VuexPage2'
import Login from '@/views/Login'
import Reg from '@/views/Reg'

Vue.use(Router)

export default new Router({
	routes: [{
			path: '/',
			name: 'Login',
			component: Login
		},
		{
			path: '/Login',
			name: 'Login',
			component: Login
		},
		{
			path: '/Reg',
			name: 'Reg',
			component: Reg
		},
		{
			path: '/AppMain',
			name: 'AppMain',
			component: AppMain,
			children: [{
					path: '/sys/Articles',
					name: 'Articles',
					component: Articles
				},
				{
					path: '/sys/VuexPage1',
					name: 'VuexPage1',
					component: VuexPage1
				},
				{
					path: '/sys/VuexPage2',
					name: 'VuexPage2',
					component: VuexPage2
				}
			]
		}
	]
})

state.js

export default{
	 resturantName:'飞歌餐馆'
}

getters.js

export default{
	getResturantName:(state)=>{
		return state.resturantName;
	}
}

mutations.js

export default{
	setResturantName:(state,payload)=>{
		state.resturantName=payload.resturantName
	}
}

显示效果
在这里插入图片描述

在这里插入图片描述

四、Vuex的异步加载问题

VuexPage1.vue

<template>
	<div>
		<ul>
			<li>这是第一张页面:{{msg}}</li>
			<li>
				<input v-model="newName" />
				<button @click="buy">盘它</button>
				<button @click="badBuy">有阴谋</button>
				<button @click="doAjax">调用ajax</button>
			</li>
		</ul>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				newName:''
			};
		},
		computed:{
			msg(){
				//不推荐,因为vuex遵循了编码解耦的原则,各模块各司其职
				return this.$store.state.resturantName;
			}
		},
		methods:{
			buy(){
				console.log(this.newName);
				this.$store.commit('setResturantName',{
					resturantName:this.newName
				})
			},
			badBuy(){
				this.$store.dispatch('setResturantNameAsync',{
					resturantName:this.newName
				})
			},
			doAjax(){
				this.$store.dispatch('doAjax',{
					_this:this
				})
			}
		}
	}
</script>

<style>

</style>

actions.js

export default{
	setResturantNameAsync:(context,payload)=>{
		console.log('xxx');
		setTimeout(()=>{
			console.log('yyy');
			context.commit('setResturantName',{
					resturantName:payload.resturantName
				})
		},6000);
		console.log('zzz');
	},
	doAjax:(context,payload)=>{
		//this.axios   不能用this,获取不到
		let _this=payload._this;
		let url = _this.axios.urls.SYSTEM_MENU_TREE;
		_this.axios.post(url, {}).then((response) => {
			console.log(response);
		}).catch(function(error) {
			console.log(error);
		});
	}
}

显示效果
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值