vuex 具体操作

action=》state=》view=》action

view中触发action

 ...mapMutations({
            showAll: 'showAll',
            showSwiper: 'showSwiper'
        }),
        clickAll(id){
            this.showAll(id);
            this.getCategoryImageList();
        },

reducer 更新状态

import {getImgList, getCategoryImageList} from '../../api/index.js';

const state = {
    list: [],
    categoryList: [],
    showDetail: false,
    isShowSwiper: false,
    id: '',    // 当前选图片目录id
    page: 1,
}

const mutations = {
    updateList(state, {list}){
        state.list = state.list.concat(list);
    },
    updateCategoryList(state, {list}){
        state.page++;
        state.categoryList = state.categoryList.concat(list);
    },
    showAll(state, payload){
        state.showDetail = true;
        state.id = payload;
    },
    showSwiper(state, payload){
        state.isShowSwiper = payload;
    }
}

const actions = {
    getImgList({commit}, payload){
        getImgList()
        .then(res=>{
            return res.json();
        })
        .then(body=>{
            console.log('body...', body);
            if (body.code == 1){
                commit('updateList', {list: body.data});
            }else{
                alert(body.msg);
            }
        })
    },
    getCategoryImageList({commit, state}, payload){
        getCategoryImageList(state.id, state.page)
        .then(res=>{
            return res.json()
        })
        .then(body=>{
            console.log('category...', body);
            if (payload && payload.cb){
                payload.cb();
            }
            if (body.code == 1){
                commit('updateCategoryList', {list: body.data.List});
            }else{
                alert(body.msg);
            }
        })
    }
}

export default {
    state,
    mutations,
    actions
}

mapActions  mapMutations mapState

   ...mapState({
            list: state=>state.img.list,
            categoryList: state=>state.img.categoryList,
            showDetail: state=>state.img.showDetail,
            isShowSwiper: state=>state.img.isShowSwiper
        }),
  ...mapActions({
            getImgList: 'getImgList',
            getCategoryImageList: 'getCategoryImageList'
        }),
 ...mapMutations({
            showAll: 'showAll',
            showSwiper: 'showSwiper'
        }),

store连接vuex与组件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值