当前位置:   article > 正文

Vuex数据管理之store使用_store文件怎么获取另一个store的state

store文件怎么获取另一个store的state

Vuex.数据数据存储
1.页面发请求,保存到state中
请求页面:
//对应组件

    async getDataFun(){
    	const res = await reqData()    //接口函数
    	this.centernameData = res.data
    	this.$store.commit('pageDataList',this.centernameData)
    }
  • 1
  • 2
  • 3
  • 4
  • 5

store:

	state:{
	  centername:'',
	}
	mutations:{
  pageDataList(state,val){
state.centernameData = val
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

接收数据页面:

this.$store.state.centername
  • 1

2.action发请求,保存数据state
请求页面:

this.$store.dispatch('getDataListAction',{centerName,page,pageSize})
  • 1

store:

state:{
centername:",
}
    actions:{
    	async getDataListAction({commit},params){
    	  const res = await reqData()    //接口函数
    	  commit('getDataList',res)
    }
    }
    mutations:{
    getDataList(state,res){
    state.centername = res   //处理具体数据改变
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.action发请求,保存大量数据时

store

state:
    	export default:{
		    user:'',
		    }
    index:
        //  vuex核心管理对象store
	       import Vue from 'vue'
		    import Vuex from 'vuex'
		    import state from './state'
		    import mutations from './mutations'
		    import actions from './actions'
		    import getters from './getters'
		    Vue.use(Vuex)
		    export default new Vuex.Store({
		      state,
		      mutations,
		      actions,
		      getters
		    })
	       

     actions:
     
     	        import {  reqUser  } from '../api'
	        import { RECEIVE_USER} from './mutation-types'
	        export default{
	    	 同步保存用户的action
	      saveUser ({commit}, user) {
	        commit(RECEIVE_USER, {user})
	      },
	          // 异步获取用户的action
	      async getUser ({commit}) {
	        const result = await reqUser()
	        if(result.code===0) {
	          const user = result.data
	          commit(RECEIVE_USER, {user})
	        }
	      },
	    }
   
   mutations:
   import Vue from 'vue'
	import {RECEIVE_USER} from './mutation-types'
	export default {
		  [RECEIVE_USER] (state, {user}) {
		    state.user = user
		  },
		  
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

请求页面:

this.$store.dispatch('getUser')
  • 1

使用数据页面:

<span class="icon-mobile-number">{{user.phone ? user.phone : '暂无绑定手机号'}}</span>
<p class="user-info-top" v-if="!user.phone">{{user.name ? user.name : '登录/注册'}} </p>
 import {mapState} from 'vuex'
 computed: {
      ...mapState(['user'])
    },

1. 2个引用变量指向同个对象, 通过一个引用变量改变对象内部的数据, 另一个引用变量看到的新的
2. 2个引用变量指向同个对象, 让一个引用变量指向一个新的对象, 另一个引用变量看到的还是原来的对象


//mapActions传参使用:
    ...mapActions({
          add: 'getNewNum' // 将 `this.add()` 映射为 `this.$store.dispatch('getNewNum')`
        })
    在 组件中进行这样调用
    
    this.add(1)
    将被映射为如下调用
    
    this.$store.dispatch('getNewNum', 1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/280132
推荐阅读
相关标签
  

闽ICP备14008679号