繁体   English   中英

Vuejs中的属性突变

[英]Property mutation in Vuejs

所以我有多个这样的代码:

<input id="data" type="text"  v-model="data">
<label for="data">Data</label>

我试图用它做一个财产,这样我就不会每次都重复它:

Vue.component('textbox', {
  template: `
   <div>
   <input :id="id" type="text" v-model="value">
   <label :for="id">{{ label }}</label>
   </div>
  `,
  props: [
      "id", "value", "label", "for"],
  watch: {
    value: function(newVal){
       this.$emit('input', newVal)
    }
  }
})

并像这样在我的html中访问它:

<textbox v-model="data" id="someID" label="Data"></textbox>

一切正常,但是每次我在文本框中键入内容时,都会在控制台中收到以下警告:

[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 component <textbox>)

有没有办法删除该警告消息? 还是我做错了方法? 任何帮助将非常感激。

您根本不需要使用手表。 而不是使用v模型,请尝试以下操作:

<input :id="id" type="text" :value="value" @input="$emit('input', $event.target.value)">

工作示例: http : //codepen.io/CodinCat/pen/dNQopR?editors=1010

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM