关键代码:
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearchAsync"
placeholder="请输入内容"
@select="handleSelect"
> <template slot-scope="{ item }">
<div class="autocomplete-choose-name" v-html="htmlValue(item.value)"></div>
</template></el-autocomplete>
<script>
export default {
data() {
return {
restaurants: [],
state: '',
timeout: null
};
},
created() {
this.restaurants = this.loadAll();
},
methods:{
htmlValue(val) {
let key = this.state;
if (key.length > 0) {
return val.replace(
new RegExp(key, "g"),
"<span style='color:red;'>" + key + "</span>"
);
}
return val;
},
loadAll() {
return [
{ "value": "三全鲜食(北新泾店)", "address": "长宁区新渔路144号" },
{ "value": "Hot honey 首尔炸鸡(仙霞路)", "address": "上海市长宁区淞虹路661号" },
{ "value": "新旺角茶餐厅", "address": "上海市普陀区真北路988号创邑金沙谷6号楼113" },
{ "value": "泷千家(天山西路店)", "address":