Skip to content

Instantly share code, notes, and snippets.

@laracasts
Created January 9, 2017 18:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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.
}
});
@nathan-isaac
Copy link

nathan-isaac commented Jan 20, 2017

I think this may have changed: https://vuejs.org/v2/guide/components.html#Form-Input-Components-using-Custom-Events

So for a component to work with v-model, it must:

  • accept a value prop
  • emit an input event with the new value

You have to pass through a prop named value instead of code.

So the component should look like this now.

Vue.component('coupon', {
    props: ['value'],

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

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

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

@rdok-pigogo
Copy link

Thanks for the clean comment @nisaac2fly ! Really helped.

@paulous
Copy link

paulous commented May 2, 2017

I just spent a few hours trying to figure out why in the video tut "code" is used instead of "value".
Thanks nathanjisaac you saved me some hair pulling :)

@prapats
Copy link

prapats commented Jul 9, 2017

Thanks @nathanjisaac for explanation. Stuck on this for a few hours too.

@corean
Copy link

corean commented Sep 15, 2017

thanks @nathanjisaac for advice. I had already spent morning

@snapjay
Copy link

snapjay commented Oct 26, 2017

Awesome @nathanjisaac; now it makes sense!

@phuocph
Copy link

phuocph commented Dec 16, 2017

I spend an hour to try to use code instead of code. Maybe it's changed. Thanks for the comment @nathanjisaac

@mokhosh
Copy link

mokhosh commented Feb 18, 2018

@nathanjisaac you da man

@ariefadjie
Copy link

Thanks @nathanjisaac, It's work use a prop named value instead of code.

@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