javascript - 当 v-model 值更改时,自定义 Vue 选择组件不更新所选选项

标签 javascript vue.js vuejs2 vue-component vue-props

我正在尝试使用 v-model 将选择包装在 Vue 自定义组件中。描述的模式in the docs .
我面临的问题是我的自定义选择组件收到以下错误消息:

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value"

found in

--->


但是,当我制作 value一个数据属性,我失去了预期的功能。也就是说,当绑定(bind)值更改时,选择框不会更新。双向绑定(bind)丢失。
在不发出警告的情况下保持我所期望的行为的正确方法是什么?
这是一个演示问题的交互式示例(最好全屏显示)。

Vue.component('dynamic-select-ex1', {
  template: '#dynamic-select-template',
  props: ['value', 'options'],
  methods: {
    changed() {
      // custom input components need to emit the input event
      this.$emit('input', event.target.value)
    },
  },
})

Vue.component('dynamic-select-ex2', {
  template: '#dynamic-select-template',
  props: ['options'],
  data() {
    return {
      value: null,
    }
  },
  methods: {
    changed() {
      // custom input components need to emit the input event
      this.$emit('input', event.target.value)
    },
  },
})

let example = new Vue({
  el: '#example',
  data() {
    return {
      selected: null,
      options: [
        { text: 'Hello', value: 1 },
        { text: 'World', value: 2 },
        { text: 'Blah', value: 3 },
        { text: 'Blerg', value: 4 },
      ]
    }
  },
  computed: {
   text() {
     if (!this.selected) return
     return this.options.find(({ value }) => value == this.selected).text
   },
  },
  methods: {
    select(value) {
      this.selected = value
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<script type="text/x-template" id="dynamic-select-template">
  <select v-model="value" @change="changed">
    <option v-for="option in options" :value="option.value">{{ option.text }}</option>
  </select>
</script>

<div id="example">
  <label for="direct">Vue behaviour for native select</label><br>
  <select id="direct" v-model="selected">
    <option v-for="option in options" :value="option.value">{{ option.text }}</option>
  </select><br>

  <div>Vue behaviour for custom component. `value` is a prop. Warning output in console when user selects option</div>
  <dynamic-select-ex1 v-model="selected" :options="options"></dynamic-select-ex1><br>

  <div>Vue behaviour for custom component. `value` is a data property. two-way binding is broken.  Selected option not updated when `value` changes.</div>
  <dynamic-select-ex2 v-model="selected" :options="options"></dynamic-select-ex2><br>
  
  <br>Selected: {{ text }}<br><br>
  
  <button @click="select(1)">Hello</button>
  <button @click="select(2)">World</button>
  <button @click="select(3)">Blah</button>
  <button @click="select(4)">Blerg</button><br>

</div>

最佳答案

计算二传手
对于 v-model , 使用 computed setterv-model而不是 value ,以避免改变 Prop 。
您可以摆脱 change听众。

<select v-model="model">
  <option v-for="option in options" :value="option.value">{{ option.text }}</option>
</select>
Vue.component('dynamic-select-ex1', {
  template: '#dynamic-select-template',
  props: ['value', 'options'],
  computed: {
    model: {
      get() { return this.value },
      set(value) { this.$emit('input', value) }
    }
  }
})
当计算值被访问时,它返回 prop 值,当它被设置时,它代替发出。
:value , @input , 和 $event.target.value另一种选择是进行value 的单向绑定(bind)。和 $emit从模板:
<select :value="value" @input="$emit('input', $event.target.value)">
  <option v-for="option in options" :value="option.value">{{ option.text }}</option>
</select>
Vue.component('dynamic-select-ex1', {
  template: '#dynamic-select-template',
  props: ['value', 'options'],
})

关于javascript - 当 v-model 值更改时,自定义 Vue 选择组件不更新所选选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66558889/

相关文章:

javascript - 使用 ng-bind-html 访问添加到 DOM 的元素

javascript - Ckeditor 选择的 html 在 chrome 浏览器中无法正常工作

css - 在 Vue 3 中全局安装 Element Plus 图标

css - NativeScript Playground - 未应用内置类

vuejs2 - VUE路由重定向未从其他页面关闭模式

css - Vuetify 图标大小

vue.js - 如果 Vue.js 中仅更改路由中的参数,如何返回按钮以触发路由保护

javascript - 即使使用惰性量词,正则表达式也保持贪婪

javascript - 根据单击的元素对不同数据使用相同的 jQuery 函数

javascript - 是否可以让 html-webpack-plugin 从 css 生成 &lt;style&gt; 元素?