【发布时间】:2021-06-22 07:24:37
【问题描述】:
import Vue from 'vue';
export default {
data() {
return {
cities : [
'Bangalore','Chennai','Cochin','Delhi','Kolkata','Mumbai'
],
value: '',
open: false,
current: 0
}
},
props: {
suggestions: {
type: Array,
required: true
},
selection: {
type: String,
required: true,
twoWay: true
}
},
computed: {
matches() {
return this.suggestions.filter((str) => {
return str.indexOf(this.selection) >= 0;
});
},
openSuggestion() {
return this.selection !== "" &&
this.matches.length != 0 &&
this.open === true;
}
},
methods: {
enter() {
this.selection = this.matches[this.current];
this.open = false;
},
up() {
if(this.current > 0)
this.current--;
},
down() {
if(this.current < this.matches.length - 1)
this.current++;
},
isActive(index) {
return index === this.current;
},
change() {
if (this.open == false) {
this.open = true;
this.current = 0;
}
},
suggestionClick(index) {
this.selection = this.matches[index];
this.open = false;
},
}
}
<template>
<div style="position:relative" v-bind:class="{'open':openSuggestion}">
<input class="form-control" type="text" v-model="selection"
@keydown.enter = 'enter'
@keydown.down = 'down'
@keydown.up = 'up'
@input = 'change'
/>
<ul class="dropdown-menu" style="width:100%">
<li
v-for="suggestion in matches"
v-bind:class="{'active': isActive($index)}"
@click="suggestionClick($index)"
>
<a href="#">{{ suggestion }}</a>
</li>
</ul>
</div>
</template>
获取 eslint 错误 [vue/require-v-for-key] 迭代中的元素期望有 'v-bind:key' directives.eslint-plugin-vue。
更改后尝试更改为 v-bind:key="suggestion.id",eslint 错误未显示但问题是自动完成未显示(完全不起作用)。
如果代码中有任何错误,谁能纠正我。
【问题讨论】:
-
suggestions和selection的数据如何? -
@RiyazKhan,过滤数据和选择的建议是为了匹配内容和检查
-
您能否分享与这两个属性完全匹配的示例数据
-
钦奈和科钦
-
返回 { 城市 : [........] }
标签: javascript vue.js