Skip to content

Instantly share code, notes, and snippets.

@anoop-ananthan
Created October 29, 2019 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoop-ananthan/ca2dd0b235777ca120742e6537559323 to your computer and use it in GitHub Desktop.
Save anoop-ananthan/ca2dd0b235777ca120742e6537559323 to your computer and use it in GitHub Desktop.
Create v-model in custom component
### Create component
SampleComponent.js
```
<template>
<div>
Mango Component:
<input @input="handleInput($event.target.value)" />
</div>
</template>
<script>
export default {
prop: ['value'],
methods: {
handleInput(e) {
this.$emit('input', e)
},
},
}
</script>
```
### Parent Component
Consume the component as follows:
###### Template
```
<sample-component v-model="dataMango"></sample-component>
<div>Parent Mango:{{ dataMango }}</div>
```
###### Data
```
import sampleComponent from '@/components/shared/ProjectWarehouse.vue'
.
.
data:()=>({
dataMango: 'Orange'
})
Find the best reference [here](https://paulund.co.uk/use-v-model-on-custom-vue-component)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment