美文网首页
操作vuex

操作vuex

作者: jack_rofer | 来源:发表于2020-10-20 17:37 被阅读0次

前沿:总结一下vuex中state,getters,mutations,actions的用法(主推map辅助函数
介绍:vuex是vue专门为了解决多组件共享数据借鉴FluxReduxThe Elm Architecture
而创造的全局单例模式管理状态库,配合devtools extension可零配置的 time-travel 调试、状态快照导入导出等高级调试功能

vuex工作模式
vuex的核心:本质就是储存数据的‘仓库’(Store),通过new创建出来,与普通全局对象不同的是,它的状态存储是响应式,还有就是不能直接改变 store 中的状态,唯一途径就是显式地提交 (commit)
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

使用前提,必须先挂载在vue实例上或直接将整个创建出来的store导入

//挂载后全局可用,this.$store.state.count
import store from './store'
new Vue({
  el: '#app',
  store
})
//直接使用,store.state.count
import store from './store'
created(){
  console.log("store.state.count",store.state.count)
}

state(就是定义全局数据)

(1)在store中
const state = {
   count: 1
}

( 2 )在页面中使用

方法①直接在html中使用

<p>{{ this.$store.state.count }}</p>

方法②利用computed获取在使用

<p>{{ count }}</p>
computed: {
  count() {
     return this.$store.state.count
  }
}

方法③利用辅助函数mapState获取在使用

<p>{{ count }}</p>
import { mapState } from 'vuex'
computed: {
  ...mapState([
       'count'   // 映射 this.count 为 this.$store.state.count
  ])
}

方法④利用辅助函数mapState获取在使用(跟③差不多)

<p>{{ count }}</p>
import { mapState } from 'vuex'
computed: mapState([ 'count'])

getters(store 的计算属性,将state中的数据装饰到页面,但不会改变原来state的数据)

( 1 )在store中
const getters = {
  count: function( state )  {
    return state.count += 100
  }
}

( 2 )在页面中使用

方法①利用computed获取

<p>{{ count }}</p>
computed: {
   count() {
      return this.$store.getters.count
  }
}

方法②利用辅助函数mapGetter获取

<p>{{ count }}</p>
import { mapGetters } from 'vuex
computed: {
   ...mapGetters([
        'count'   //把 `this.count ` 映射为 `this.$store.getters.count `
   ])
}

mutations(提交或者说改变state中的数据,用commit)

( 1 )在store中

const mutations={
    add(state){
        state.count++;
    }
}

( 2 )在页面中使用

方法①在html中使用直接commit

<button @click="$store.commit('add')">+</button>

方法②利用辅助函数mapMutations 获取

<button @click="add">+</button>
import { mapMutations } from 'vuex
methods: {
    ...mapMutations([  
        'add',  //将 `this.add()` 映射为 `this.$store.commit('add')`
    ]),
}

actions(作用其实是调用mutations,因为数据来源在后端必定是异步,异步操作全局状态)

( 1 )在store中

方法①context(上下文对象,这里你可以理解称store本身)

const actions ={
    addAction(context){
        context.commit('add',10)
    }
}

方法②{ commit }(直接把commit对象传递过来,可以让方法体逻辑和代码更清晰明了)

const actions ={
    addAction({commit}){
        commit('add')
    }
}

( 2 )在页面中使用(dispatch)

方法①直接在html中使用

<button @click="$store.dispatch('addAction')">+</button>

方法②利用辅助函数mapMutations 获取

<button @click="addAction">+</button>
import { mapMutations } from 'vuex
methods:{
    ...mapActions([
       'addAction',    //将 `this.addAction()` 映射为 `this.$store.dispatch('addAction')`
    ])
},

module

( 1 )在store中
将某个功能或页面的所有属性放到一个模块

const moduleA={
    state,mutations,getters,actions
}

export default new Vuex.Store({
    modules:{a:moduleA}
})

( 2 )在页面中使用

方法①直接computed拿

computed:{
    count(){
        return this.$store.state.a.count;
    }
},

方法②

小结:
1.vuex是什么玩意,其实它就是个壳,我们要的是里面的肉Store。
2.new 出来的store里面就有state(数据),getters(计算),mutations(同步改数据),actions(异步改数据)。
3.只要全局挂载了store,就可以在各个页面中去操作store里面的属性。
4.使用辅助函数是最简单最直观 的方法操作store了,但是要理解一下具体表示啥。
5.触发mutations函数直接调用store.commit方法($store.commit)
6.actons调用mutations函数:context.commit
7.涉及到多组件传值直接用vuex吧,别想那么多,其也没别人说的那么臃肿
8.多个页面都要用到store要分块,好管理

进阶:
1.当有模块嵌套起来,则在每个子模块的getter和actions的参数就是暴露出rootState

const moduleA = {
  // rootState为根状态
  getters: {
    sumWithRootCount (state, getters, rootState) {
      return state.count + rootState.count
    }
  },
  actions: {
    incrementIfOddOnRootSum ({ state, commit, rootState }) { //此时 state, commit, rootState应该是在context对象里面的
      if ((state.count + rootState.count) % 2 === 1) {
        commit('increment')
      }
    }
  }
}

参考:官网
参考: vuex免费视频教程

相关文章

  • Vuex学习(一)--项目引用实际操作(基础)

    实际操作步骤: 1.引入Vuex (1)安装vuex:npm install vuex --save (2)在sr...

  • Vuex_状态管理04(Actions)

    vuex规定mutation中对于数据的操作必须是同步操作,如果你对于store中的数据操作是异步的,要用vuex...

  • 操作vuex

    前沿:总结一下vuex中state,getters,mutations,actions的用法(主推map辅助函数)...

  • vuex 使用教程

    vuex简介 vuex相当于一个浏览器的一个本地存储,不过vuex拥有更复杂的操作,vuex包含了数据定义,数据过...

  • 使用Vuex,modules的命名空间 报错 unknown a

    使用Vuex 报错 unknown action type:XXX(未知的操作类型: Vuex命名空间的报错 h...

  • Vuex

    Vuex Vuex:用于管理数据的工具(对象),读写数据 初始化 在组件中使用Vuex 鉴于以上操作较复杂,vue...

  • 【融职培训】Web前端学习 第7章 Vue基础教程11 vuex

    一、Vuex概述 二、基于Vuex的计数器 我们不能直接修改state,需要定义mutation来操作state,...

  • vuex2.x初始

    vuex自己的理解 vuex是公共状态管理库,我把他理解为公共操作库。我的思路是这样:多个组件有着同样的操作,我们...

  • vuex详细操作

    只讲操作,不谈原理,适合工作用,不适合面试 一个最基本的vuex可以由一个js文件来实现,也是我们使用脚手架初始化...

  • Vuex 详细操作

    前言 上一章,讲解了 vuex 的入门,主要阐述了 state、getters、mutations、actions...

网友评论

      本文标题:操作vuex

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