0

I am using Vue.js and Vue-Select without node.js. I am importing them directly into my html file like so:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>
<script src="https://npmcdn.com/vue-select@latest"></script>

I have a v-select menu defined in <body> with an on-change option which should call hello()

<v-select on-change="hello" :options="['foo','bar','baz', 'asdf']"></v-select>

And the Vue-Select component is registered like this, with the hello() method;

<script type="text/javascript">

      Vue.component('v-select', VueSelect.VueSelect);

        new Vue({
          el: 'body',
          methods: {
            hello (val) {
              alert(val)
            }
          }
        });

</script>

But alas nothing happens and I get the following error message in console:

vue.js:990 [Vue warn]: Invalid prop: type check failed for onChange="hello". Expected Function, got String.

This is based on the on-change callback event described in the vue-select documentation... but it doesn't work.

I would just like to to create an event when the select box is changed; i.e. I would like pass the value of the select box to a function which does something.

2
  • What is the error you get, how are you using it, add that code. Commented Feb 1, 2017 at 11:32
  • i have updated thanks Commented Feb 1, 2017 at 13:20

2 Answers 2

1

I believe your issue is in your template, you need to bind the on-change property otherwise it's just sending a string to the instance.

<v-select v-on:on-change="hello" :options="['foo','bar','baz', 'asdf']"></v-select>

or if you prefer shorthand.

<v-select @on-change="hello" :options="['foo','bar','baz', 'asdf']"></v-select>

EDIT:

Ok I'm a bit of a moron not noticing the binding method you need to use v-bind or the : shorthand as seen here jsFiddle looking at the v-select code on github I noticed they're explicitly calling the function instead of emitting it.

so your html should be

<v-select v-bind:on-change="hello" :options="['foo','bar','baz', 'asdf']"></v-select>
<v-select :on-change="hello" :options="['foo','bar','baz', 'asdf']"></v-select>
Sign up to request clarification or add additional context in comments.

3 Comments

That should not be an issue, that's how it is in the documentation as well.
@Saurabh I've yet to see a case where that works the way it shows in that documentation, in the Vuejs.org Event Handling Docs it specifies the need for v-on or the @ shorthand when passing functions or expressions to a callback. As well if you notice the vue-warn @eli gives us is that it was expecting a function and received a string.
I tried making some fiddles to test with Question gives the vue-warn Our Answers don't yet the function isn't called. Which is an oddity
0

As there is a validation in the code, for onchange to be a function, You should try one with function syntax, like following:

  Vue.component('v-select', VueSelect.VueSelect);

    new Vue({
      el: 'body',
      methods: {
        hello: function(val) {
          alert(val)
        }
      }
    });

3 Comments

I tried this but still get the same error (vue.js:990 [Vue warn]: Invalid prop: type check failed for onChange="hello". Expected Function, got String.)
@eli I am not sure you can do el: 'body, you have to give that div an id, and use that id here, that might tne the issue.
el:body works (the select box is shown) and it's done that way in the vue.js documentation. using a div made no difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.