【发布时间】:2021-09-11 13:27:58
【问题描述】:
我有一个通常运行良好的自定义输入组件。但是从昨天开始,我想在某些情况下从父组件传递一个值(即,如果为该字段找到 cookie,则使用 cookie 值预填充该字段)。
父组件(简化):
<custom-input
v-model="userEmail"
value="John Doe"
/>
但由于某种我无法理解的原因,value 道具不起作用。为什么不呢?
我的自定义输入组件(简化版):
<template>
<input
v-bind="$attrs"
:value="value"
@blur="handleBlur"
>
</template>
<script>
export default {
inheritAttrs: false,
props: {
value: {
type: String,
default: ''
}
},
mounted () {
console.log(this.value) // displays nothing, whereas it should display "John Doe"
},
methods: {
handleBlur (e) {
this.$emit('input', e.target.value)
}
}
}
</script>
【问题讨论】: