首页前端开发VUEvuex的几个属性及其使用传参方式

vuex的几个属性及其使用传参方式

时间2024-02-11 04:50:03发布访客分类VUE浏览681
导读:收集整理的这篇文章主要介绍了vuex的几个属性及其使用传参方式,觉得挺不错的,现在分享给大家,也给大家做个参考。 目录vuex概念1.1、组件之间共享数据的方式1.2、Vuex是什么1....
收集整理的这篇文章主要介绍了vuex的几个属性及其使用传参方式,觉得挺不错的,现在分享给大家,也给大家做个参考。
目录
  • vuex概念
    • 1.1、组件之间共享数据的方式
    • 1.2、Vuex是什么
    • 1.3、使用Vuex同意管理状态的好处
  • vuex的基本使用
    • vuex的核心概念
      • State
      • Mutation
        • 第一种方式
        • 第二种方式
      • Action
        • 第一种方式
        • 第二种方式
      • **Getter **

      vuex概念

      1.1、组件之间共享数据的方式

      父 向 子 传值:v-bind属性绑值

      子 向 父 传值:v-on事件绑定

      兄弟组件之间共享数据:EventBus

      • $on 接收数据的那个组件
      • $emIT 发送数据的那个组件

      1.2、Vuex是什么

      Vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。

      1.3、使用Vuex同意管理状态的好处

      能够在vuex中集中管理共享的数据,易于开发和后期维护

      能够高效地实现组件之间的数据共享,提高开发效率

      存储在vuex中的数据都是响应式的,能够实时保持数据与页面的同步。

      1.4、什么样的数据适合存储到Vuex中

      一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依然存储在组件自身的data中即可。

      vuex的基本使用

      1、安装vuex依赖包

      npm i vuex --save

      2、导入vuex包

      import Vuex From 'vuex'Vue.use(Vuex)

      3、创建stroe对象

      const Store = new Vuex.Store({
          //state中存放的就是全局共享的数据	state:{
      count:0}
      }
          )

      4、将store对象挂载到vue实例中

      new Vue({
          	el:'#app',	render:h =>
       h(app),	router,	//将创建的共享数据对象,挂载到vue实例中	//所有的组件,就可以直接从stroe中获取全局的数据了	store}
          )

      vuex的核心概念

      1、vuex中的主要核心概念如下

      State

      State提供唯一的公共数据源, 所有共享的数据都要统放到Store的State中进行存储。

      export default new Vuex.Store({
        state: {
          count: 0  }
      ,}
          )

      组件访问State中数据的**第一种方式:**Addition.vue

      this.$store.state.全局数据名称h3>
      当前最新的count值为:{
      {
      $store.state.count}
      }
          /h3>
          

      组件访问State中数据的第二种方式: SuBTraction.vue

      // 1. 从 vuex 中按需导入 mapState 函数import {
       mapState }
           from 'vuex'

      通过刚才导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的 computed 计算属性:

      h3>
      当前最新的count值为:{
      {
      count}
      }
          /h3>
      // 2. 将全局数据,映射为当前组件的计算属性computed: {
       ...mapState(['count'])}
          

      Mutation

      Mutation 用于变更 Store中 的数据。

      只能通过 mutation 变更 Store 数据,不可以直接操作 Store 中的数据。

      ② 通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化。

      第一种方式

      (1)

      // 定义 Mutation    store.js const store = new Vuex.Store({
       	state: {
       		count: 0 	}
      ,	mutations: {
       		add(state) {
           		// 不要在 mutations 函数中,执行异步操作      	// setTimeout(() =>
       {
            	//   state.count++      	// }
      , 1000)	 	// 变更状态	 	state.count++	 	}
       	}
       }
          )

      在组件中访问Mutation Addition.vue

       button @click="btnHandler1">
          +1/button>
      methods:{
      	//触发mutation    btnHandler1() {
      	  //触发 mutations 的第一种方式      this.$store.COMmit('add')    }
      ,}
          

      (2)可以在触发 mutations 时传递参数:

       // 定义Mutation const store = new Vuex.Store({
       	state: {
       	count: 0 	}
      , 	mutations: {
      		 addN(state, step) {
       			// 变更状态			 state.count += step		 }
       	}
       }
          )

      在组件中访问Mutation Addition.vue

       button @click="btnHandler2">
          +N/button>
         methods: {
          btnHandler2() {
            // commit 的作用,就是调用 某个 mutation 函数  携带参数      this.$store.commit('addN', 3)    }
      ,  }
          

      第二种方式

      this.$store.commit() 是触发 mutations 的第一种方式,触发 mutations 的第二种方式:

      // 1. 从 vuex 中按需导入 mapMutations 函数import {
       mapMutations }
           from 'vuex'

      通过刚才导入的 mapMutations 函数,将需要的 mutations 函数,映射为当前组件的 methods 方法:

      // 2. 将指定的 mutations 函数,映射为当前组件的 methods 函数methods: {
       ...mapMutations(['add', 'addN'])}
          

      完整代码:

      //store.jsexport default new Vuex.Store({
        state: {
          count: 0  }
      ,  // 只有 mutations 中定义的函数,才有权利修改 state 中的数据  mutations: {
          //减减    sub(state) {
            state.count--    }
      ,      //携带参数    subN(state, step) {
            state.count -= step    }
        }
      ,}
          )

      在组件中Subtraction.vue

      h3>
      当前最新的count值为:{
      {
      count}
      }
          /h3>
          button @click="btnHandler1">
          -1/button>
          //button @click="btnHandler2">
          -1/button>
          button @click="subN(3)">
          -N/button>
                  -----携带参数import {
       mapState, mapMutations}
       from 'vuex' computed: {
          ...mapState(['count']),  }
      ,  methods: {
          ...mapMutations(['sub', 'subN']),    btnHandler1() {
            this.sub()    }
      , 	//btnHandler2() {
            //this.subN(3)    //}
      ,  }
          

      Action

      (1)Action 用于处理异步任务。

      如果通过异步操作变更数据,**必须通过 Action,而不能使用 Mutation,**但是在 Action 中还是要通过触发Mutation 的方式间接变更数据。

      第一种方式

      (1)处理异步任务

      // 定义 Action const store = new Vuex.Store({
      	 // ...省略其他代码	 mutations: {
       		add(state) {
       			state.count++ 		}
       	}
      , 	actions: {
       		addAsync(context) {
           			setTimeout(() =>
       {
       				context.commit('add') 			}
      , 1000) 		}
        	}
       }
          )

      在组件中Addition.vue

      button @click="btnHandler3">
          +1 Async/button>
      // 异步地让 count 自增 +1btnHandler3() {
          // 这里的 dispatch 函数,专门用来触发 action    this.$store.dispatch('addAsync')}
          ,

      (2)携带参数

      触发 actions 异步任务时携带参数:

      // 定义 Action const store = new Vuex.Store({
       	// ...省略其他代码 	mutations: {
       		addN(state, step) {
              -------------携带参数			 state.count += step 		}
       	}
      , 	actions: {
       		addNAsync(context, step) {
             -------------携带参数 			setTimeout(() =>
       {
       				context.commit('addN', step) 			}
      , 1000) 		}
        	}
       }
          )

      在组件中

      button @click="btnHandler4">
          +N Async/button>
      btnHandler4() {
            this.$store.dispatch('addNAsync', 5)}
          

      第二种方式

      this.$store.dispatch() 是触发 actions 的第一种方式,触发 actions 的第二种方式:

      // 1. 从 vuex 中按需导入 mapActions 函数import {
       mapActions }
           from 'vuex'

      通过刚才导入的 mapActions 函数,将需要的 actions 函数,映射为当前组件的 methods 方法:

      // 2. 将指定的 actions 函数,映射为当前组件的 methods 函数methods: {
       ...mapActions(['addASync', 'addNASync'])}
          

      完整代码

      export default new Vuex.Store({
        state: {
          count: 0  }
      ,  // 只有 mutations 中定义的函数,才有权利修改 state 中的数据  mutations: {
          //减减    sub(state) {
            state.count--    }
      ,    subN(state, step) {
            state.count -= step    }
        }
      ,  //actions可以处理异步任务  actions: {
          subAsync(context) {
                setTimeout(() =>
       {
              // 在 actions 中,不能直接修改 state 中的数据;        // 必须通过 context.commit() 触发某个 mutation 才行        context.commit('sub')      }
      , 1000)    }
      ,    subNAsync(context, step) {
                setTimeout(() =>
       {
              context.commit('subN', step)      }
      , 1000)    }
        }
      ,}
          )

      组件Subtraction.vue

      h3>
      当前最新的count值为:{
      {
      count}
      }
          button @click="subAsync">
          -1 Async/button>
          button @click="subNAsync(5)">
          -N Async/button>
       import {
       mapState, mapMutations, mapActions }
       from 'vuex'methods: {
          ...mapActions(['subAsync', 'subNAsync']),}
          

      **Getter **

      不会修改state里面的数据

      Getter 用于对 Store 中的数据进行加工处理形成新的数据。

      ① Getter 可以对 Store 中已有的数据加工处理之后形成新的数据,类似 Vue 的计算属性。

      ② Store 中数据发生变化,Getter 的数据也会跟着变化。

      // 定义 Getter const store = new Vuex.Store({
      	state: {
       		count: 0 	}
      , 	getters: {
           		showNum: state =>
       {
       			return '当前最新的数量是【'+ state.count +'】' 		}
       	}
       }
          )

      使用getters的第一种方式

      this.$store.getters.名称

      使用getters的第一种方式

      {
      {
      showNum}
      }
      import {
       mapGetters }
       from 'vuex'computed: {
       ...mapGetters(['showNum'])}
          
      • 只有 mutations 中定义的函数,才有权利修改 state 中的数据
      • actions 中,不能直接修改 state 中的数据必须通过 context.commit() 触发某个 mutation 才行
      • commit 的作用,就是调用 某个 mutation 函数
      • dispatch 函数,专门用来触发 action

      到此这篇关于vuex的几个属性及其使用传参的文章就介绍到这了,更多相关vuex属性使用传参内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

      您可能感兴趣的文章:
      • Vuex给state中的对象新添加属性遇到的问题及解决
      • 几分钟弄懂Vuex的五大属性和使用方式
      • 详解Vuex的属性
      • vuex中的state属性解析
      • 详解Vuex的属性和作用
      • 说说Vuex的getters属性的具体用法
      • Vuex的actions属性的具体使用
      • Vue唯一可以更改vuex实例中state数据状态的属性对象Mutation的讲解

      声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


      若转载请注明出处: vuex的几个属性及其使用传参方式
      本文地址: https://pptw.com/jishu/609341.html
      vue element-ui el-table组件自定义合计(summary-method)的坑 vue前端性能优化之预加载和懒加载示例详解

      游客 回复需填写必要信息