您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

谈谈我对vuex的理解

2024/4/2 19:08:37发布29次查看
vuex是解决vue组件和组件间相互通信而存在的,vuex理解起来稍微复杂,但一旦看懂则即为好用:
安装:
npm install --save vuex
引入
import vuex
vuex的几个参数的介绍
state         储存初始化数据
getters      对state 里面的数据二次处理(对数据进行过滤类似filter的作用)比如state返回的为一个对象,我们想取对象中一个键的值用这个方法
mutations   对数据进行计算的方法全部写在里面(类似computed) 在页面中触发时使用this.$store.commit('mutationname') 触发mutations方法改变state的值
actions      处理mutations中已经写好的方法 其直接触发方式是 this.$store.dispatch(actionname)
我们先不急着了解更多先打印下vuex
console.log(vuex) //vuex为一个对象里面包含vuex ={     store:function store(){},         mapactions:function(){},    // 对应actions的结果集mapgetters:function(){},    //对应getters的结果集mapmutations:function(){},  //对应mutations的结果集mapstate:function(){},      //对应state的结果集install:function install(){}, //暂时不做讲解 installed:true //暂时不做讲解}//如果我们只需要里面的state时我们可以这样写import { mapstate } from 'vuex'; import { mapgetters, mapmutations } from 'vuex'; //如果需要引用多个时用这种方式处理
反复看上面的内容是不是就豁然开朗了接下来我们进行依次举例和用官方的语言描述
state
state负责存储整个应用的状态数据,一般需要在使用的时候在跟节点注入store对象,后期就可以使用this.$store.state直接获取状态
//store为实例化生成的import store from './store' new vue({   el: '#app',   store,   render: h => h(app) })
这个store可以理解为一个容器,包含着应用中的state等。实例化生成store的过程是:
//./store文件const store = new vuex.store({   state: {   //放置state的值     count: 0,     strlength:abcd234   },   getters: {   //放置getters方法       strlength: state => state.astring.length   },   mutations: {   //放置mutations方法        mutationname(state) {          //在这里改变state中的数据  state.count = 100;        }   },  // 异步的数据操作  actions: {      //放置actions方法        actionname({ commit }) {          //dosomething commit('mutationname')       },       getsong ({commit}, id) {           api.getmusicurlresource(id).then(res => {             let url = res.data.data[0].url;                    })           .catch((error) => {  // 错误处理              console.log(error);           });       }   } }); export default store;
后续在组件中使用的过程中,如果想要获取对应的状态你就可以直接使用this.$store.state获取,当然,也可以利用vuex提供的mapstate辅助函数将state映射到计算属性中去,如
import {mapstate} from 'vuex'export default {  //组件中   computed: mapstate({     count: state => state.count   }) }
getters
有些状态需要做二次处理,就可以使用getters。通过this.$store.getters.valuename对派生出来的状态进行访问。或者直接使用辅助函数mapgetters将其映射到本地计算属性中去。
在组件中使用方式
import {mapgetters} from 'vuex'export default {   computed: mapgetters(['strlength']) }
mutations
mutations的中文意思是“变化”,利用它可以更改状态,本质就是用来处理数据的函数,其接收唯一参数值state。store.commit(mutationname)是用来触发一个mutation的方法。需要记住的是,定义的mutation必须是同步函数,否则devtool中的数据将可能出现问题,使状态改变变得难以跟踪。
在组件中触发:
export default {   methods: {     handleclick() {      this.$store.commit('mutationname')     }   } }
或者使用辅助函数mapmutations直接将触发函数映射到methods上,这样就能在元素事件绑定上直接使用了。如:
import {mapmutations} from 'vuex'export default {   methods: mapmutations(['mutationname'   ]) }
actions
actions也可以用于改变状态,不过是通过触发mutation实现的,重要的是可以包含异步操作。其辅助函数是mapactions与mapmutations类似,也是绑定在组件的methods上的。如果选择直接触发的话,使用this.$store.dispatch(actionname)方法。
在组件中使用
import {mapactions} from 'vuex'//我是一个组件export default {   methods: mapactions(['actionname',   ]) }
plugins
插件就是一个钩子函数,在初始化store的时候引入即可。比较常用的是内置的logger插件,用于作为调试使用。
//写在./store文件中import createlogger from 'vuex/dist/logger'const store = vuex.store({   ...   plugins: [createlogger()] })
以上就是谈谈我对vuex的理解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product