我刚刚安装了vue-instant来为搜索提供自动建议,并获得这样的示例代码
https://jsfiddle.net/santiblanko/dqo6vr57/
我的问题是如何将组件'vue-instant': VueInstant.VueInstant
移动到新的Vue组件,如下所示:
Vue.component('vue-instant-component', {
//vue-instant
}
我尝试过这样的事情:
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)
});
});
}
}
})
但它不起作用
答案 0 :(得分:6)
我稍微误解了这个问题,下面是原来的答案。
您可以将上面的代码转换为组件:
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
}
})
原始回答
我只想将上面的示例移动到表单Vue.component()。代码不应该在新的Vue()中,而应该在Vue.component()
中
如果我理解正确,您要查找的语法是
Vue.component('vue-instant', VueInstant.VueInstant)
更新了fiddle。