Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 计算属性不为';t在只读输入上更新_Vue.js_Vuex - Fatal编程技术网

Vue.js 计算属性不为';t在只读输入上更新

Vue.js 计算属性不为';t在只读输入上更新,vue.js,vuex,Vue.js,Vuex,我有一个默认设置为只读的输入字段,当您单击按钮时,应该可以编辑该项 如果没有readonly字段,则该字段似乎更新正确 没有它,我编辑项目,当焦点丢失时,它会恢复到以前的文本 <template> <input ref="input" :readonly="!edit" type="text" v-model="inputValue" @blur="edit = false"> <but

我有一个默认设置为只读的输入字段,当您单击按钮时,应该可以编辑该项

如果没有readonly字段,则该字段似乎更新正确

没有它,我编辑项目,当焦点丢失时,它会恢复到以前的文本

<template>
  <input ref="input"
         :readonly="!edit"
         type="text"
         v-model="inputValue"
         @blur="edit = false">
  <button @click="editItem"/>
</template>

data(){
  return {
    edit: false,
  }
}
methods: {
  ...mapMutations(['setKey']),
  editItem() {
    this.edit = true;
    this.$nextTick( () => this.$refs.input.focus() );
  }
}
computed: {
  ...mapGetters(['getKey']),
  inputValue: {
    get() {
      return this.getKey();
    }
    set(value) {
      this.setKey({value});
    }
  }
}

在我看来,有一种更好的方法来处理textfield值。使用MapGetter+mapActions。将getter值与
一起使用:value=inputValue
。并通过您的操作在blur上提交此值。顺便说一句,请不要更改html模板中的数据(
edit=false
)<代码>:只读适用于
真/假
。 或者你可以用watcher。Smth是这样的:

v-model="inputValue",
@blur="toggleEgit"

...
data () {
    return {
        inputValue: this.getKey
    }
},

methods: {
    toggleEgit () {
        this.isEdit = !this.isEdit;
    }
},

watch: {
    inputValue: {
          handler: function (value) {
              this.$commit('setKey', value) // or whatever you want to update your store
          }
    }
}

在我看来,有一种更好的方法来处理textfield值。使用MapGetter+mapActions。将getter值与
一起使用:value=inputValue
。并通过您的操作在blur上提交此值。顺便说一句,请不要更改html模板中的数据(
edit=false
)<代码>:只读适用于
真/假
。 或者你可以用watcher。Smth是这样的:

v-model="inputValue",
@blur="toggleEgit"

...
data () {
    return {
        inputValue: this.getKey
    }
},

methods: {
    toggleEgit () {
        this.isEdit = !this.isEdit;
    }
},

watch: {
    inputValue: {
          handler: function (value) {
              this.$commit('setKey', value) // or whatever you want to update your store
          }
    }
}

如您所见,我已经在使用MapGetter和mapActions来更改文本字段值。是否有任何理由从MapTranslations移动到mapActions?问题是没有调用计算的get,数据更新正确。操作是异步的,而不是。也许你应该在getter上使用watcher?在他们自己的例子中,他们使用的是提交,所以我不确定这是否是问题所在。但是如果没有readonly属性,一切都可以工作,并且调用getter。同样,使用watcher而不是set/get。这很简单,prob将解决您的问题,正如您所看到的,我已经在使用MapGetter和mapActions来更改文本字段值。是否有任何理由从MapTranslations移动到mapActions?问题是没有调用计算的get,数据更新正确。操作是异步的,而不是。也许你应该在getter上使用watcher?在他们自己的例子中,他们使用的是提交,所以我不确定这是否是问题所在。但是如果没有readonly属性,一切都可以工作,并且调用getter。同样,使用watcher而不是set/get。这很容易,prob会解决你的问题