哈哈嘟嘟嘟
哈哈嘟嘟嘟
  • 发布:2023-03-16 11:51
  • 更新:2023-03-18 01:22
  • 阅读:143

nvue中vuex无法同步state的值,该怎么办

分类:HBuilderX

同样的代码vue是正常的

<template>  
    <view class="content">  
        <view @click="add">  
            {{username}}  
        </view>  
    </view>  
</template>  

<script>  
    import store from '@/store/index.js'  
    import { mapState } from 'vuex'  
    export default {  
        data() {  
            return {  

            }  
        },  
        computed: {  
            ...mapState({  
                username: state => state.username,  
            })  
        },  
        methods: {  
            add(x){  
                store.commit('add')  
            }  
        }  
    }  
</script>
import Vue from 'vue'  
import Vuex from 'vuex'  

Vue.use(Vuex); //vue的插件机制  

//Vuex.Store 构造器选项  
const store = new Vuex.Store({  
    state: { //存放状态  
        "username": "foo",  
        "age": 18  
    },  
    mutations: {  
        add(state) {  
            state.username ='change' //方法触发了,但是页面没有改变  
        }  
    }  
})  
export default store  
2023-03-16 11:51 负责人:无 分享
已邀请:
小黑子爱rap

小黑子爱rap - jack

换mapMutaions试一下?

  • 哈哈嘟嘟嘟 (作者)

    试过了,一样

    2023-03-20 09:35

  • 小黑子爱rap

    回复 x***@163.com: state: { //存放状态

    "username": "foo",

    "age": 18

    },

    这个"username",“age” 去掉引号试一下; 说不定就好了。。。

    2023-03-20 09:48

要回复问题请先登录注册