I am changing my code from the template file to the render function. Now I have this HTML:
:open.sync="state"
But I don't know how to translate this to JavaScript. How to write this to the createElement function?
I am changing my code from the template file to the render function. Now I have this HTML:
:open.sync="state"
But I don't know how to translate this to JavaScript. How to write this to the createElement function?
Remember that
:open.sync="state"
is basically syntax sugar for
:open="state" @update:open="state = $event"
Then the equivalent in the render function would be:
createElement('child', {
props: { // :open="state"
"open": this.state
},
on: { // @update:open="state = $event"
"update:open": ($event) => {
this.state = $event;
}
}
})
Demo:
Vue.component('child', {
template: '#child',
props: ['open']
})
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!'
},
render(createElement) {
return (
createElement('div', [
this.message,
createElement('br'),
createElement('child', {
props: {
"open": this.message
},
on: {
"update:open": ($event) => {
this.message = $event;
}
}
})
])
);
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<p>{{ message }}</p>
<child :open.sync="message"></child>
</div>
<template id="child">
<div>
<input type="text" :value="open" @input="$emit('update:open', $event.target.value)">
open: {{ open }}
</div>
</template>
As March 2021 && From @acdcjunior inspiration I've came up with this solution:
// From Parent
<custom-input
:open.sync="formData.groupName"
/>
// CHILD
<div class="wrapper">
<input
type="text"
class="full-width"
:value="open" @input="$emit('update:open', $event)"
/>
</div>
BTW: His solutions wasn't working since $event.target.value was undefined.