vue.js - RE:vue-bootstrap-typeahead API(序列化器)
问题描述
我正在制作一个使用vue-bootstrap-typeahead的 vue 应用程序。它的 API 参考中有一部分使用serializer
,特别是对于来自外部资源的下拉建议。有人可以进一步向我解释一下吗?一直在网上寻找答案,但我似乎无法让它工作。这是我的代码片段。
<template>
<div class="container">
<vue-bootstrap-typeahead
v-model="query"
placeholder="search for someone"
:serializer="input => input"
:data="names"
/>
</div>
</template>
<script>
export default {
data() {
return {
query: '',
selectedUser: null,
names: []
}
}
},
watch: {
query(newQuery) {
axios.get(`api/participant?fName=${newQuery}`)
.then((res) => {
this.users = res.data.items
})
}
}
</script>
当我使用预定义的数据数组进行建议时,它可以工作。但是当尝试从资源中查询时,它不起作用。你们之前有没有使用过这个包的经验?请帮忙。谢谢你。
解决方案
:serializer="input => input" 意味着您的 api 返回了一些对象数组,对吗?然后你只需将元素显示为喜欢 toString(),这样你的 api 应该返回一个字符串数组。如果 api 返回例如: [{Id:1, Desc: "One"}, {Id:2, Desc:"Two"}] 你应该放在那里 :serializer="input => input.Desc"
推荐阅读
- azure - 是否有 Powershell/Azure-CLI 甚至是门户选项来复制公式以在 Azure 中创建 VM?
- mysql - 将连接数组传递给laravel中的查询
- arrays - 如何有效地将 unsigned char* 传递给 C++/CLI 中的托管方法?
- laravel-5 - 从 laravel 将用户添加到 phpbb3 论坛用户表
- postgresql - 创建表时出错:“@”处或附近的语法错误
- java - 在 java servlet 中设置多种内容类型
- python - Twitter api 流不是将推文作为推文对象传递,而是作为字符串传递
- python-2.7 - 如何使用点群制作复杂的形状......比如椅子、火箭等等,使用 pygame 和 numpy
- javascript - 为什么使用缓冲区转换日期格式比使用字符串操作慢?
- windows - 如何通过 CMD 在 Windows 上仅删除用户路径中的某些值