美文网首页
vuex简单使用

vuex简单使用

作者: 变量只提升声明不提升赋值 | 来源:发表于2020-11-14 11:47 被阅读0次

vuex是一个数据集中管理的库,在数据量庞大并且涉及到多个组件之间交互使用数据的时候,他就可以派上用场,完美的解决了组件之间数据交互的赋杂性,大家都可以直接从vuex中访问到想要的数据

image.png

思维图中,state就表示公共的数据池,vue commponents就代表着组件,而actions责是修改数据的函数,但要注意的是他并不直接修改数据,而是通过调用mutations中的函数去修改数据
组件们通过dispatch去分发通知,actions中的函数则通过commit方法来调用mutations中的方法,具体用法如下方实例

首先安装vuex
npm install vuex
新建一个store.js文件用于创建store
在store中引入vuex

/*
vuex最核心的管理对象store
 */
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

/**
 * 数据池
 * @type {{}}
 */
const state = {
  count: 0
}
/**
 *计算属性
 */
const getters = {

}
/**
 * 更改state中数据的方法
 * @type {{}}
 */
const mutations = {
}
/**
 * 通知mutations更改数据的方法
 * @type {{}}
 */
const actions = {
}

export default new Vuex.Store({
  state,  //状态对象,相当于一个数据池data
  mutations, // 包含多个更新state函数的对象
  actions, //包含多个对应事件回调函数的对象
  getters //包含多个getter计算属性函数的对象
})

同时需要在入口文件中将store映射到vue 实例上,以便后续的使用

import store from './store'

new Vue({
  el: '#app',
  components: {
    App
  },
  template: '<App/>',
  store // 注册上vuex的store: 所有组件对象都多一个属性$store  同$router一样以在每个组件中使用$store来访问store中的东西
               
})
如下一个简单的计数器,count是一个公共的数据,我们将他定义在store中
,在模板里通过$store.state.count来渲染页面   

  <div>
    <p>click {{$store.state.count}} times, count is {{evenOrOdd}}</p>

    <button @click="increment">+</button>
    <button @click="decrement">-</button>
    <button @click="incrementIfOdd">increment if odd</button>
    <button @click="incrementAsync">increment async</button>
  </div>


 computed: {
      evenOrOdd () {
        this.$store.getters.evenOrOdd  //调用store里的计算属性,不需要带括号,因为这里是去读取这个计算属性的值
      }
    },

    methods: {
      increment () {
        this.$store.dispatch('increment')  //通过dispatch去分发通知,
      },
      decrement () {
        this.$store.dispatch('decrement')
      },
      incrementIfOdd () {
        this.$store.dispatch('incrementIfOdd')
      },
      incrementAsync () {
        this.$store.dispatch('incrementAsync')
      }
    }

store.js

/**
 * 数据池
 * @type {{}}
 */
const state = {
  count: 0
}

/**
 *计算属性
 */
const getters = {
  //自带一个state,可供访问state中的数据
  evenOrOdd (state) {
    return state.count % 2 == 1 ? '奇数' : '偶数'
  }
}

/**
 * 更改state中数据的方法
 * @type {{}}
 */
const mutations = {
  increments(state,val){
    console.log(state,val)
    state.count++
  },
  decrements(state){
    state.count--
  },
  incrementIfOdds(state){
    state.count++
  },
  incrementAsyncs(state){
    state.count++
  }
}
/**
 * 通知mutations更改数据的方法
 * @type {{}}
 */
const actions = {
//actions中的方法会接收一个对象作为参数,commit是一个方法,state是store里的state
  increment({commit,state}){
    commit('increments',659)
  },
  decrement({commit,state}){
    commit('decrements')
  },
  incrementIfOdd({commit,state}){
    if (state.count%2==1){
      commit('incrementIfOdds')
    } else{
      alert('不是奇数')
    }
  },
  incrementAsync({commit}){
    setTimeout(()=>{
      commit('incrementAsyncs')
    },1000)
  }
}

store中的state和getters都是可以通过 this.store.state 或者 this.store.getters 直接访问到的,,

而想要修改state中的值那么就需要一个点击事件来分发这个通知给actions

通过store提供的dispatch去分发通知,这个和$emit语法一样,传一个事件名称,后面还可以跟参数
 this.$store.dispatch('increment')  //通过dispatch去分发通知,
store.js的actions对象中,需要去定义这个方法,这个方法会
接收一个对象对位参数,对象里有commit方法和state,
state就是store.js中的state  commit就是去调用mutations中的方法

increment({commit,state}){
    commit('increments',659)
  },

而mutations对象中也要定义一个相应的方法来去修改state中的值

/**
 * 更改state中数据的方法
 * @type {{}}
 */
const mutations = {
  increments(state,val){
    console.log(state,val)
    state.count++
  },
}

总之,在组件中访问store中的数据和计算方法通过this.store就可以直接访问到,想要修改state中的值需要先通过this.store.dispacth.xxx发送通知给actions中的方法 然后actions中的方法通过commit方法去调用mutations中的方法来修改数据, actions不修改数据,只是调用mutations来修改数据,真正修改数据的是mutations中的方法

相关文章

  • 通过一个简单实例了解vuex

    简单说明 什么是vuex,vuex怎么使用,什么场景下适合使用vuex, vuex 文档中都有介绍。看完文档之后,...

  • vuex使用记录

    副标题:vuex使用详解、vue使用全局变量、vue使用 store 这篇博客主要记录了 vuex的使用方法,简单...

  • vuex简单使用

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式,本文会介绍怎么使用vuex,比较简单,如果想对vu...

  • vuex简单使用

    https://vuex.vuejs.org/zh/installation.html[https://vuex....

  • vuex简单使用

    vuex是一个数据集中管理的库,在数据量庞大并且涉及到多个组件之间交互使用数据的时候,他就可以派上用场,完美的解决...

  • Vuex简单使用

    一、初始化Vuex Vuex[https://vuex.vuejs.org/zh/]是一个专为 Vue.js 应用...

  • 2020-03-25 vuex 全集

    Vuex入门(1)—— Vuex的设计初衷和简单使用 https://blog.csdn.net/dkr38020...

  • vuex简单简单使用记录

    1、Vuex有啥用(非官方解释)举例,组件a b 使用了同一个数据源count,当操作a的时候count++,同时...

  • vuex入门详解

    vuex最简单、最详细的入门文档 vuex最简单、最详细的入门文档 如果你在使用 vue.js , 那么我想你可能...

  • vuex记录

    前言 官方文档讲vuex讲的很清楚了,本文用于总结vuex的简单使用 简介 Vuex 是一个专为 Vue.js 应...

网友评论

      本文标题:vuex简单使用

      本文链接:https://www.haomeiwen.com/subject/dinibktx.html