我刚刚安装了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)
});
});
}
}
})
但它不起作用
components
块,你可以在构建Vue
实例之前调用Vue.component('vue-instant', VueInstant.VueInstant);
来注册它。 - BotjesuggestionAttribute未定义
。 - Abid RakhmansyahVueInstant
并带有一些默认设置吗? - BotjeVue.component()
表单中。代码不应该在new Vue()
中,而是在Vue.component()
中。 - Abid Rakhmansyah