admin管理员组文章数量:1399509
I am trying to create dynamic input ponent that will be interchangeable between input and textarea tag. I am trying to implement this by using render function. (.html#v-model).
The problem I have is that v-model works only one way, if I change data property directly it updates textarea value, but if I change or input new data into textarea it does not update the data property. Does anyone know how to make it work both ways? Here is my code link for code pen is bellow it illustrates the problem:
const tag = Vueponent('dynamic-tag', {
name: 'dynamic-tag',
render(createElement) {
return createElement(
this.tag,
{
domProps: {
value: this.value
},
on: {
input: event => {
this.value = event.target.value
}
}
},
this.$slots.default
)
},
props: {
tag: {
type: String,
required: true
}
}
})
const app = new Vue({
el: '#app',
data: {
message: ''
},
ponents: {tag}
})
I am trying to create dynamic input ponent that will be interchangeable between input and textarea tag. I am trying to implement this by using render function. (https://v2.vuejs/v2/guide/render-function.html#v-model).
The problem I have is that v-model works only one way, if I change data property directly it updates textarea value, but if I change or input new data into textarea it does not update the data property. Does anyone know how to make it work both ways? Here is my code link for code pen is bellow it illustrates the problem:
const tag = Vue.ponent('dynamic-tag', {
name: 'dynamic-tag',
render(createElement) {
return createElement(
this.tag,
{
domProps: {
value: this.value
},
on: {
input: event => {
this.value = event.target.value
}
}
},
this.$slots.default
)
},
props: {
tag: {
type: String,
required: true
}
}
})
const app = new Vue({
el: '#app',
data: {
message: ''
},
ponents: {tag}
})
http://codepen.io/asolopovas/pen/OpWVxa?editors=1011
Share Improve this question edited Jul 13, 2022 at 23:02 tony19 139k23 gold badges277 silver badges347 bronze badges asked Mar 8, 2017 at 16:26 Andrius SolopovasAndrius Solopovas 1,05717 silver badges49 bronze badges2 Answers
Reset to default 5You need to $emit
changes from your input.
on: {
input: event => {
this.value = event.target.value
this.$emit('input', event.target.value)
}
}
The problem is that you are using v-model with a custom ponent.
When using with ponent v-model="message"
is just syntactic sugar for
v-bind:value="message"
v-on:input="value => { message = value }"
So to use v-model with custom ponent, your ponent must have value
prop and emit input event with the changed value.
I will leave further explanations to the documentation
本文标签: javascriptvmodel implementation via render function is not reactiveStack Overflow
版权声明:本文标题:javascript - v-model implementation via render function is not reactive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744208243a2595293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论