b***@gmail.com
b***@gmail.com
  • 发布:2019-01-16 16:37
  • 更新:2019-01-16 17:29
  • 阅读:9679

vuex state数据更新了 mapState mapGetters未生效

分类:uni-app

点击login后, vuex数据发生变化, 页面上没变化

<template>  
    <view class="content">  
        <button type="primary" @click="login22">登录</button>  
        <text class="title">{{getUserInfo?getUserInfo.name:""}}---{{getUserInfo?getUserInfo.avatar:""}}</text>  
        <text class="title">{{user2?user2.name:""}}---{{user2?user2.avatar:""}}</text>  
    </view>  
</template>  

<script>  
import { mapGetters, mapState } from 'vuex';  
export default {  
    data() {  
        return {  
            user: {  
                name: '',  
                avatar: ''  
            }  
        };  
    },  
    computed: {  
        ...mapGetters(['getUserInfo']),  
        ...mapState({ user2: state => state.user })  
    },  
    methods: {  
        login22() {  
            this.user.name = 'admin';  
            this.user.avatar = 'url';  
            this.$store.dispatch('login', this.user);  
        }  
    }  
};  
</script>  

<style>  
</style>  

vuex:
index.js

import Vue from 'vue'  
import Vuex from 'vuex'  
import actions from './actions'  
import mutations from './mutations'  

Vue.use(Vuex)  

export default new Vuex.Store({  
    actions,  
    modules: {  
        mutations  
    }  
})  

actions.js

const actions = {  
    login({commit}, userInfo) {  
        commit("login", userInfo)  
    }  
}  

export default actions  

mutations.js

import getters from './gettes.js'  
const state = {  
    user: {  
        name: 'aa',  
        avatar: 'cc'  
    }  
}  

const mutations = {  
    login(state, userInfo) {  
        this.state.user = userInfo  
        console.log(this.state)  
    }  
}  

export default {  
    state,  
    mutations,  
    getters  
}

getters.js

export default {  
    getUserInfo(state) {  
        return state.user  
    }  
}
2019-01-16 16:37 负责人:无 分享
已邀请:
b***@gmail.com

b***@gmail.com (作者)

搞定了 mutations里边修改state状态不对, 不应该加 [this.]state

b***@gmail.com

b***@gmail.com (作者)

vuex的问题, mapgetters和mapstate不是这么用的

Trust

Trust - 少说废话

直接上传下项目,并描述下操作步骤。

  • b***@gmail.com (作者)

    我再看看 mapGetters解决了 mapState还不行

    2019-01-16 17:24

该问题目前已经被锁定, 无法添加新回复