是否可以在 element-ui el-autocomplete 回调中传递数组?

Can an Array be passed in an element-ui el-autocomplete callback?

我尝试使用具有最简单版本的 el-autocomplete 标记:回调函数 (JSFiddle version) 返回的 Array

Vue.component('button-counter', {
  data: function() {
    return {
      selectdusers: [],
      user: ''
    }
  },
  template: '<el-container>        <el-container style="margin-left: 15px; width: 150px">            <el-autocomplete                class="inline-input"                v-model="user"                :fetch-suggestions="filterUsers"                placeholder="add user"                @select="handleSelect"            ></el-autocomplete>            <el-button type="info" icon="el-icon-plus" click="addUser(user)">Add</el-button>        </el-container>    </el-container>',
  methods: {
    addUser(user) {
      this.selectedUsers.push(user)
      this.user = ''
    },
    filterUsers(user, cb) {
      console.log(user)
      cb(['qqq', 'zzz'])
      //return this.allusers.filter(x => _.includes(x.f+x.t, user))
    },
    handleSelect() {}
  },
})

new Vue({
  el: '#components-demo'
})
@import url("//unpkg.com/element-ui@2.4.8/lib/theme-chalk/index.css");
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.4.7/index.js"></script>
<div id="components-demo">
  <button-counter></button-counter>
</div>

尽管 DevTools 在 <ElAutocompleteSuggestions>datasuggestions

中显示了预期的元素,但建议框在视觉上是空的

需要通过回调传递的是一个 Object,它必须有一个名为 value 的 属性。此 属性 包含显示为建议的内容。

对于上面的例子:

filterUsers(user, cb) {
      console.log(user)
      cb([{'value': 'qqq', 'whatever': 'djhjfh'}, {'value': 'zzz', 'whatever': 'uiuiiu'}])
    },

要使用 Array 而不是 Object,您可以在自动完成块中使用 slot

<el-autocomplete ...>
  <template slot-scope="{ item }">
    <div class="item">{{ item }}</div>
  </template>
</el-autocomplete>