Vuex

本文深入探讨了Vue.js应用程序中的状态管理模式Vuex,讲解了如何使用Vuex在多个组件间共享和管理数据,包括State、Getters、Mutations、Actions和Modules等核心概念。

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

Vuex

1 . vue中各个组件之间传值
1.父子组件
父组件–>子组件,通过子组件的自定义属性:props
子组件–>父组件,通过自定义事件:this.$emit(‘事件名’,参数1,参数2,…);

2.非父子组件或父子组件
通过数据总数Bus,this.root.root.root.emit(‘事件名’,参数1,参数2,…)

3.非父子组件或父子组件
更好的方式是在vue中使用vuex

方法1: 用组件之间通讯。这样写很麻烦,并且写着写着,估计自己都不知道这是啥了,很容易写晕。
方法2: 我们定义全局变量。模块a的数据赋值给全局变量x。然后模块b获取x。这样我们就很容易获取到数据

2 . Vuex是什么
官方解释:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。可以想象为一个“前端数据库”(数据仓库),
让其在各个页面上实现数据的共享包括状态,并且可操作
在这里插入图片描述
Vuex在这里插入图片描述
Vuex的五个部分:

   1.State:单一状态树
   2.Getters:状态获取
   3.Mutations:触发同步事件
   4.Actions:提交mutation,可以包含异步操作
   5.Module:将vuex进行分模块

首先在命令窗口输入以下代码

    npm install vuex -S

创建store模块,分别维护state/actions/mutations/getters

        index.js
        state.js
        actions.js
        mutations.js
        getters.js

在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

在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'
import 'element-ui/lib/theme-chalk/index.css'
// process.env.MOCK && require('@/mock')
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import axios from '@/api/http' 
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取值
state.js:

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

VuexPage1.vue:

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

<script>
	export default {
		data() {
			return {
				
			};
		},
		computed:{
			msg(){
				//不推荐,Vuex遵循了解耦的原则,各模块各司其职
				return this.$store.state.resturantName;
			}
		}
	}
</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>

Vuex存值
mutation.js:

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

VuePage1.vue:

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

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

<style>

</style>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
同步和异步
actions.js:

export default {
	setResturantNameAsync: (content, payload) => {
		console.log('xxx');
		setTimeout(() => {
			console.log('yyy');
			content.commit('setResturantName', {
				resturantName: payload.resturantName
			})
		}, 6000);
		console.log('zzz');
	}
}

VuexPage1.vue:

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

<script>
	export default {
		data() {
			return {
			  newName:''
			};
		},
		computed:{
			msg(){
				//不推荐,Vuex遵循了解耦的原则,各模块各司其职
				return this.$store.getters.getResturantName;
			}
		},
		methods:{
			buy(){
				this.$store.commit('setResturantName',{
					resturantName:this.newName
				})
			},
			badBuy(){
				this.$store.dispatch('setResturantNameAsync',{
					resturantName:this.newName
				})
							
			 }
			}
	}
</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)=>{
		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);
		});
}
}
内容概要:本文详细探讨了杯形谐波减速器的齿廓修形方法及寿命预测分析。文章首先介绍了针对柔轮与波发生器装配时出现的啮合干涉问题,提出了一种柔轮齿廓修形方法。通过有限元法装配仿真确定修形量,并对修形后的柔轮进行装配和运转有限元分析。基于Miner线性疲劳理论,使用Fe-safe软件预测柔轮寿命。结果显示,修形后柔轮装配最大应力从962.2 MPa降至532.7 MPa,负载运转应力为609.9 MPa,解决了啮合干涉问题,柔轮寿命循环次数达到4.28×10⁶次。此外,文中还提供了详细的Python代码实现及ANSYS APDL脚本,用于柔轮变形分析、齿廓修形设计、有限元验证和疲劳寿命预测。 适合人群:机械工程领域的研究人员、工程师,尤其是从事精密传动系统设计和分析的专业人士。 使用场景及目标:①解决杯形谐波减速器中柔轮与波发生器装配时的啮合干涉问题;②通过优化齿廓修形提高柔轮的力学性能和使用寿命;③利用有限元分析和疲劳寿命预测技术评估修形效果,确保设计方案的可靠性和可行性。 阅读建议:本文涉及大量有限元分析和疲劳寿命预测的具体实现细节,建议读者具备一定的机械工程基础知识和有限元分析经验。同时,读者可以通过提供的Python代码和ANSYS APDL脚本进行实际操作和验证,加深对修形方法和技术路线的理解。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值