Skip to content

Instantly share code, notes, and snippets.

@laracasts
Created January 9, 2017 18:16
Show Gist options
  • Save laracasts/37117786ec23bfacf391d81c2cefa35b to your computer and use it in GitHub Desktop.
Save laracasts/37117786ec23bfacf391d81c2cefa35b to your computer and use it in GitHub Desktop.
vuecasts.com - Custom Input Components exercise.
Vue.component('coupon', {
props: ['code'],
template: `
<input type="text"
:value="code"
@input="updateCode($event.target.value)"
ref="input">
`,
methods: {
updateCode(code) {
// Atttach validation + sanitization here.
this.$emit('input', code);
}
}
});
new Vue({
el: '#app',
data: {
coupon: 'FREEBIE' // Maybe from DB or querystring.
}
});
@inuk66
Copy link

inuk66 commented Aug 14, 2018

The original component should also work with just a small modification:

Vue.component('coupon', {
    // New.
    model: {
        prop: 'code'
    },

    props: ['code'],

    template: `
        <input type="text"
               :value="code"
               @input="updateCode($event.target.value)"
               ref="input">
    `,

    methods: {
        updateCode(code) {
            // Atttach validation + sanitization here.

            this.$emit('input', code);
        }
    }
});

https://vuejs.org/v2/guide/components-custom-events.html#Customizing-Component-v-model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment