12

I just installed vue-instant to make an auto suggestion for search and get an example code like this

https://jsfiddle.net/santiblanko/dqo6vr57/

My question is how to move components 'vue-instant': VueInstant.VueInstant to a new Vue component like this one:

Vue.component('vue-instant-component', {
  //vue-instant
}

I've tried something like this:

Vue.component('vue-instant', {
  data: {
    value: '',
    suggestionAttribute: 'original_title',
    suggestions: [],
    selectedEvent: ""
  },
  methods: {
    clickInput: function() {
      this.selectedEvent = 'click input'
    },
    clickButton: function() {
      this.selectedEvent = 'click button'
    },
    selected: function() {
      this.selectedEvent = 'selection changed'
    },
    enter: function() {
      this.selectedEvent = 'enter'
    },
    keyUp: function() {
      this.selectedEvent = 'keyup pressed'
    },
    keyDown: function() {
      this.selectedEvent = 'keyDown pressed'
    },
    keyRight: function() {
      this.selectedEvent = 'keyRight pressed'
    },
    clear: function() {
      this.selectedEvent = 'clear input'
    },
    escape: function() {
      this.selectedEvent = 'escape'
    },
    changed: function() {
      var that = this;
      this.suggestions = [];
      axios.get('https://api.themoviedb.org/3/search/movie?api_key=342d3061b70d2747a1e159ae9a7e9a36&query=' + this.value)
        .then(function(response) {
          response.data.results.forEach(function(a) {
            that.suggestions.push(a)
          });
        });
    }
  }
})

but it doesn't work

4
  • What are you trying to do? If you just want to avoid the components block at the end of your fiddle, you can call Vue.component('vue-instant', VueInstant.VueInstant); before constructing the Vue instance to register it. Commented Sep 19, 2017 at 15:18
  • @Botje, it give me suggestionAttribute is not defined Commented Sep 19, 2017 at 15:21
  • Then I'm lost as to what you are trying to do. Are you trying to create a new component that extends VueInstant with some defaults? Commented Sep 19, 2017 at 15:23
  • @Botje I just want to move the example above to the form Vue.component () . The code should not in new Vue() but in Vue.component () Commented Sep 19, 2017 at 15:28

1 Answer 1

8

I slightly misunderstood the question, below is the original answer.

This is how you might turn the code above into a component:

Vue.component("movies",{
  template:`
    <div>
      {{selectedEvent}}
      <vue-instant :suggestion-attribute="suggestionAttribute" v-model="value" :disabled="false"  @input="changed" @click-input="clickInput" @click-button="clickButton" @selected="selected"  @enter="enter" @key-up="keyUp" @key-down="keyDown" @key-right="keyRight" @clear="clear"  @escape="escape" :show-autocomplete="true" :autofocus="false" :suggestions="suggestions" name="customName" placeholder="custom placeholder" type="google"></vue-instant>
    </div>
  `,
  data(){
    return {
      value: '',
      suggestionAttribute: 'original_title',
      suggestions: [],
      selectedEvent: ""
    }
  },
  methods: {
    clickInput: function() {
      this.selectedEvent = 'click input'
    },
    clickButton: function() {
      this.selectedEvent = 'click button'
    },
    selected: function() {
      this.selectedEvent = 'selection changed'
    },
    enter: function() {
      this.selectedEvent = 'enter'
    },
    keyUp: function() {
      this.selectedEvent = 'keyup pressed'
    },
    keyDown: function() {
      this.selectedEvent = 'keyDown pressed'
    },
    keyRight: function() {
      this.selectedEvent = 'keyRight pressed'
    },
    clear: function() {
      this.selectedEvent = 'clear input'
    },
    escape: function() {
      this.selectedEvent = 'escape'
    },
    changed: function() {
      var that = this
      this.suggestions = []
      axios.get('https://api.themoviedb.org/3/search/movie?api_key=342d3061b70d2747a1e159ae9a7e9a36&query=' + this.value)
        .then(function(response) {
        response.data.results.forEach(function(a) {
          that.suggestions.push(a)
        })
      })
    }
  },
  components: {
    'vue-instant': VueInstant.VueInstant
  }
})

Original answer

I just want to move the example above to the form Vue.component () . The code should not in new Vue() but in Vue.component ()

If I understand correctly, the syntax you are looking for is

Vue.component('vue-instant', VueInstant.VueInstant)

Updated fiddle.

Sign up to request clarification or add additional context in comments.

3 Comments

It work when i placed it on <div id="app"> . But when I put inside a component error occurs
@AbidRakhmansyah Did you want to include all those handlers in the component?
Right :) Thank you sir!

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.