在input元素中时,相当于:value :input
<input v-model="ipt" />
<input :value="ipt" @input="ipt = $event.target.value" />
因为 input 元素本身有个 oninput 事件,是 H5 新增加的,类似 onchange ,当输入框内容发生变化时,就会触发oninput,把最新的value传递给 sth
v-model用在组件上时,父向子传递数据,子接收使用
props: {
value: { type: String, default: '' }
},
子向父传递使用$emit(‘input’,)
this.$emit('input', urlImg)
在组件中使用 v-model 属性时,默认会把 value 作为组件的属性,然后把 ‘input’ 值作为给组件绑定事件时的事件名
<div id="demo">
<currency-input v-model="price">
</currency-input>
<currency-input :value="price" @input="price = arguments[0]"></currency-input>
<span>{{price}}</span>
</div>
<script src="https://cdn.bootcss.com/vue/2.3.0/vue.js"></script>
<script>
Vue.component('currency-input', {
template: `
<span>
<input
ref="input"
:value="value"
@input="$emit('input', $event.target.value)"
>
</span>
`,
props: ['value'], // 为什么这里要用 value 属性,value在哪里定义的?貌似没找到啊?
})
// $event.target.value指向当前input值
// vm.$emit( event, arg )
// arguments[0]也是input值
var demo = new Vue({
el: '#demo',
data: {
price: 100,
}
})
</script>