我刚刚安装了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)
          });
        });
    }
  }
})

但它不起作用

推荐答案

我对这个问题有点误解,以下是原始答案.

以下是将上述代码转换为组件的方式:

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

我只想把上面的例子移到表格Vue.组件().代码不应该在new Vue()中,而应该在Vue中.组件()

如果我理解正确,你要找的语法是

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

更新fiddle.

Vue.js相关问答推荐

VUE:带自定义组件的数组输入绑定

Nuxt3 / Vue3 - 当属性更改时如何动态更改渲染组件?

在 Vuejs 中更新 Chartjs 图表数据集

在计算(computed)属性上调用数组方法

如何使用 vue 或 javascript 向服务器发送密码?

vuejs:将props传递给javascript中的组件?

使用上传组件而不触发 POST 请求

如何使用 v-on:change 将输入文本传递给我的 vue 方法?

Vue-router:循环路由以动态创建

VueJS + Typescript:类型上不存在属性

Vue.js 处理多次点击事件

如何从 vue.js 捕获 Jquery 事件

按键删除对象不会更新vue组件

带有后退按钮的 Vue.js 路由

如何让 Vue 在 shadow dom 中工作

如何在 JEST 测试中模拟全局 Vue.js 变量

依赖关系甚至在 package.json 和 node_modules 中都没有定义

如何将props传递给组件的故事?

在 Vuejs 中全局导入 Axios 方法

在 Vue 中使用事件修饰符 .prevent 提交表单而不进行重定向