v-model with custom element that wrap checkbox.
See original GitHub issueHi,
I am using vuejs 2.0.1 and I want to create a component that looks like this:
<vue-checkbox v-model="page.ads_able"></vue-checkbox>
As from the api page that v-model is a syntactic sugar combinding :value
and @input
.
So I write a template with a props name value and a method for input event like this:
...
<input type="checkbox" :value="value" @input="onInput" />
...
...
props: {
value: {},
},
methods: {
onInput(event) {
this.$emit('input', event.target.value)
}
}
...
But it doesn’t work. I don’t know if there any special care about checked attribute of the checkbox. I tried the exact same thing with <select>
and it’s worked as aspect though. Any suggestion?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Vue: binding with v-model in custom checkbox component ...
When you do this.$emit('input', e.target.value);. it works like myCheckBoxModel = e.target.value. So it just assigns the value of the last ...
Read more >Using v-model with checkbox in custom component - Vue Forum
I'm having trouble figuring out how to properly make use of v-model within a custom component that has a checkbox input. Using v-model...
Read more >Create a Custom Checkbox Component with Vue - Muhi Masri
In this article I'd like to share with you how to create a reusable custom checkbox element using CSS and VueJS.
Read more >Vue two way data-binding in custom checkbox
Let's start creating our component. It will be as simple as just rendering the checkbox and a label can be set through props....
Read more >Creating Custom Inputs With Vue.js - Smashing Magazine
Learn how to create custom checkboxes and radios that emulate how v-model works on them natively. Quick note before we get started: ES2015+...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well, I think I find a solution to my own question.
I need to set a data variable to accept value from props and change that data at the corresponding event then emit the input event so that v-model can handle the value change. Something along these lines:
template
And in javascript:
That’s work now. 😄
Sorry for beign late. I came up with this solution, what do you guys think?
Code demo here: https://jsfiddle.net/ariel0196/euzn79gc/3/