PHP Linter 与规则定义小探

前言:

初次尝试使用vuex,记录在vue2项目中如何使用vuex进行数据管理,以便日后温习。 vue2项目中,要使用vuex的3版本;vue3项目中,要使用vuex的4版本。该文没有记录安装vuex的步骤,是因为在创建项目的时候选择了vuex选择项。

使用步骤:

1.在根目录下创建store文件夹,并创建index.js文件(基本结构如下

// 引入vue
import Vue from 'vue'
// 引入vuex
import Vuex from 'vuex'
// 应用Vux插件
Vue.use(Vuex)

//1. state用于存放数据
const state = {}
// 2.getter用于state中数据加工
const getters = {}
// 3.mutations用于操作数据
const mutations = {}
// 4.actions用于响应组件中的动作
const actions = {}
// 创建并暴露store
export default new Vuex.Store({state,getter,mutations,actions
}) 

2.在main.js文件中引入store对象并进行挂载

3.在组件中进行使用。

Demo演示:

1.创建index.js文件

// 引入vue
import Vue from 'vue'
// 引入vuex
import Vuex from 'vuex'
// 应用Vux插件
Vue.use(Vuex)

//1. state用于存放数据
const state = {name: 'LeeYUFan',sex: '男',age: 20 //周岁
}
// 2.getter用于state中数据加工
const getters = {nominalAge(state) {return state.age + 1}
}
// 3.mutations用于操作数据
const mutations = {// 修改姓名updateName(state, val) {console.log(state, val)state.name = val},// 修改性别updateSex(state, val) {state.sex = val},updateAge(state,val){state.age = val}
}
// 4.actions用于响应组件中的动作
const actions = {// 两秒之后进行性别的修改delayUpdateSex(content, val) {// 直接通过改变state中的数据// content.state.sex = val// 通过调用mutations对象中的函数对state中的数据进行操作setTimeout(() => {content.commit('updateSex', val)console.log('我是3秒后才执行的函数')}, 3000)}
}
// 创建并暴露store
export default new Vuex.Store({state,getters,mutations,actions
}) 

2.在main.js文件中引入store对象

import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 引入store
import store from './store'

Vue.config.productionTip = false

new Vue({router,store,render: h => h(App)
}).$mount('#app') 

3.在组件中进行使用。

测试: 在main.js文件中进行引入并挂载后,在组件中输出this,结果如下:(每个组件实例上都包含着Store对象)

在组件中使用:

<template><div id="app"><HelloWorld></HelloWorld><!-- 读取store中数据 --><h4>姓名:{{this.$store.state.name}}</h4><h5>性别:{{this.$store.state.sex}}</h5><h5>周岁:{{this.$store.state.age}}</h5><h5>虚岁:{{nominalAge}}</h5><button @click="updateName">修改姓名</button><button @click="updateSex">修改性别</button><button @click="updateAge">修改年龄</button><button @click="delayUpdateSex">异步修改呢性别</button></div>
</template>
<script> import HelloWorld from './components/HelloWorld.vue'export default{components:{HelloWorld},mounted(){console.log(this,'App中输出this>>>>>>>>>>>>>>>')},computed:{// 虚岁nominalAge(){this.$store.getters.nominalAge}},methods:{// 修改store中数据updateName(){this.$store.commit('updateName','李玉帆')},// 修改性别updateSex(){this.$store.commit('updateSex','女')},// 修改年龄updateAge(){this.$store.commit('updateAge',21)},// 异步进行性别修改delayUpdateSex(){this.$store.dispatch('delayUpdateSex','Female')}}} </script> 

借助辅助函数在组件中使用:

<template><div id="app"><HelloWorld></HelloWorld><!-- 读取store中数据 --><h4>姓名:{{name}}</h4><h5>性别:{{sex}}</h5><h5>周岁:{{age}}</h5><h5>虚岁:{{age1}}</h5><button @click="updateName('李玉帆')">修改姓名</button><button @click="updateSex('女')">修改性别</button><button @click="updateAge(21)">修改年龄</button><button @click="delayUpdateSex('Female')">异步修改呢性别</button></div>
</template>
<script> import HelloWorld from './components/HelloWorld.vue'import { mapGetters, mapState ,mapMutations,mapActions} from 'vuex'export default{components:{HelloWorld},mounted(){console.log(this,'App中输出this>>>>>>>>>>>>>>>')},computed:{// 借助mapState生成计算属性 ---- 对象的写法...mapState({name:'name',sex:'sex',age:'age'}),// 借助mapGetters生成计算属性---- 对象的写法...mapGetters({age1:'nominalAge'}),// 数组的写法 (键名和值相同时支持数组的写法)// ...mapState(['name','sex','age'])},methods:{...mapMutations(['updateName','updateSex','updateAge']), ...mapActions(['delayUpdateSex'])}} </script> 

注意:

1.借助辅助函数时,如要写成数组的形式,需要保证组件中方法名字和index.js文件中方法的名字是一致的。

2.使用辅助函数进行传参

3.借助辅助函数读取state中的数据等同于

4.通过actions对象中的方法直接操作state是可以进行的,但是通过vue调试者工具不可以观察到数据的变化。

最后

最近找到一个VUE的文档,它将VUE的各个知识点进行了总结,整理成了《Vue 开发必须知道的36个技巧》。内容比较详实,对各个知识点的讲解也十分到位。



有需要的小伙伴,可以点击下方卡片领取,无偿分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值