JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了vue.js – 带有setter的mapState大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过mapState分配setter方法.我目前使用一种解决方法,我将我感兴趣的变量(todo)命名为临时名称(storetodo),然后在另一个计算变量todo中引用它. @H_547_2@methods: { ...mapMutations([ 'clearTodo','updateTodo' ]) },computed: { ...mapState({ storetodo: state => state.todos.todo }),todo: { get () { return this.storetodo},set (value) { this.updateTodo(value) } } }

我想跳过额外的步骤并直接在mapState中定义getter,setter.

我为什么要这样做?

通常的方法是使用mapMutations / mapActions& mapState / mapGetters
没有我上面说明的计算得到/设置组合,并直接在HTML中引用变异:

<input v-model='todo' v-on:keyup.stop='updateTodo($event.target.value)' />

getter / setter版本允许我简单地写:

<input v-model='todo' />

解决方法

您不能在mapState中使用getter / setter格式

您可以尝试直接返回get()中的状态并从computed属性中删除mapState

computed: {
    todo: {
        get () { return this.$store.state.todos.todo},set (value) { this.updateTodo(value) }
    }
}

这是一个相关但不相同的JsFiddle example

大佬总结

以上是大佬教程为你收集整理的vue.js – 带有setter的mapState全部内容,希望文章能够帮你解决vue.js – 带有setter的mapState所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。