Vuex请求访问,组件获取返回值

组件里获取Vuex里state存的值为空

在Vuex的actions方法用Axios请求访问接口时,返回的数据需要在组件里获取,但是会出现为空的情况。那是因为在组件里面的正确逻辑是先派发方法,获取到数据后再赋值调用,所以应该用异步的方法。如下是完整代码:(大致功能是删除一条数据,接口返回成功或失败的提示,根据系统返回的提示,弹出提示框)
store文件里:

import Axios from "axios";
const state={
   //更改列表受影响时返回的信息
    BaoXainShouYingXiang:''
}
const getters={
	 //获取数据的受影响
    getbaoxainshouyingxiang(state){
        return state.BaoXainShouYingXiang
    }
}
const actions={
	//    删除数据
    getbaoxianDelAction({commit},baoXianGuanLiId){
    	return new Promise((resolve, reject) => {
        	Axios.post('/api/yilipei/baodan/delete?baoXianGuanLiId='+baoXianGuanLiId.id+'',{
            }).then((res)=>{
                commit('delbaoxian',res.data.msg)
                resolve('')
            })
        })
    }
}
const mutations={
	//    删除数据
    delbaoxian(state,delShouYinXiang){
        state.BaoXainShouYingXiang=delShouYinXiang
    }
}
export default {
    state,
    getters,
    actions,
    mutations
}

组件里面:

<script>
// import baoxianList from "@/components/wang/baoxianList";
import {mapGetters}  from "vuex"
export default {
  name: "baoxianManage",
  components:{
  },
  data(){
    return{
      ShouYIngXiang:''
    }
  },
  methods:{
    //删除数据
    delbaoxian(id){
      this.id={id}
      console.log(this.id)
      this.ShouYIngXiang=''
      this.$store.dispatch('getbaoxianDelAction', this.id).then(()=>{
        this.ShouYIngXiang=this.getbaoxainshouyingxiang
        this.$message(''+this.ShouYIngXiang+'');
        this.$store.dispatch('getbaoxainListAction')
      })
    },
  computed:{
    ...mapGetters(['getbaoxainshouyingxiang'])
  },
  created() {
    this.$store.dispatch('getbaoxainListAction',this.pageNum)
  }
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值