Vue algolia autosuggest on select 显示 [Object object]
Vue algolia autosuggest on select showing [Object object]
TLDR
点击建议后如何设置 vue-autosuggest
的值?
我正在使用一个相对简单的应用程序 ais-instasearch along with vue-autosuggest。我的应用程序按预期工作,但我遇到的问题是,当我 select 建议中的一个项目时,它将我的输入字段设置为 [Object object]
,我无法从文档中弄清楚,也不调试我应该 return 的值,以便我可以清除输入字段。就我的代码而言,inout 框未映射到任何 v-model
s。
当我检查值集时,例如我搜索 lol
,我看到 ais-autoComplete
组件在其状态下具有值 lol
。
我的组件是
<ais-instant-search :search-client="searchClient" index-name="blogs">
<ais-configure
:hitsPerPage="showing"
:restrictSearchableAttributes="searchCategories"
/>
<ais-autocomplete :classNames="{'ais-Autocomplete': 'tools-searchbar'}">
<template slot-scope="{ currentRefinement, indices, refine }">
<vue-autosuggest
:suggestions="indicesToSuggestions(indices)"
@selected="onSelect"
:input-props="{
id: 'tools-searchbar',
onInputChange: refine,
placeholder: 'Search for blogs...',
}"
>
<template slot-scope="{ suggestion }">
<a id="tools-link" target="_blank">
<v-flex>
<v-avatar class="pr-2" size="36">
<img
:src="suggestion.item.image || '/blogs.png'"
alt="/blogs.png"
/>
</v-avatar>
<span id="tool-name">
<ais-highlight
:hit="suggestion.item"
attribute="resolved_title"
v-if="suggestion.item.resolved_title"
/>
</span>
<div id="tool-description">
<ais-highlight
:hit="suggestion.item"
attribute="excerpt"
v-if="suggestion.item.excerpt"
/>
</div>
</v-flex>
</a>
</template>
</vue-autosuggest>
</template>
</ais-autocomplete>
</ais-instant-search>
我的方法是:
methods: {
onSelect(selected) {
if (selected) {
this.query = "";
window.open(selected.item.resolved_url);
}
},
indicesToSuggestions(indices) {
if (this.loggedin) {
return indices.map(({ hits }) => ({ data: hits }));
}
},
第一张图片是点击匹配结果前的图片。
第二张是点击匹配结果后的图片。
点击后显示值为lol
的组件
根据 Vue-autosuggest 文档:
https://github.com/darrenjennings/vue-autosuggest#getsuggestionvalue
使用道具 get-suggestion-value 并将方法绑定到 return 您希望从该建议中获得的任何属性。
<vue-autosuggest
v-model="query"
:suggestions="filteredOptions"
@focus="focusMe"
@click="clickHandler"
@input="onInputChange"
@selected="onSelected"
:get-suggestion-value="getSuggestionValue"
:input-props="{id:'autosuggest__input', placeholder:'Do you feel lucky, punk?'}">
<div slot-scope="{suggestion}" style="display: flex; align-items: center;">
<img :style="{ display: 'flex', width: '25px', height: '25px', borderRadius: '15px', marginRight: '10px'}" :src="suggestion.item.avatar" />
<div style="{ display: 'flex', color: 'navyblue'}">{{suggestion.item.name}}</div>
</div>
</vue-autosuggest>
并且,
getSuggestionValue(suggestion) {
return suggestion.item.name;
},
TLDR
点击建议后如何设置 vue-autosuggest
的值?
我正在使用一个相对简单的应用程序 ais-instasearch along with vue-autosuggest。我的应用程序按预期工作,但我遇到的问题是,当我 select 建议中的一个项目时,它将我的输入字段设置为 [Object object]
,我无法从文档中弄清楚,也不调试我应该 return 的值,以便我可以清除输入字段。就我的代码而言,inout 框未映射到任何 v-model
s。
当我检查值集时,例如我搜索 lol
,我看到 ais-autoComplete
组件在其状态下具有值 lol
。
我的组件是
<ais-instant-search :search-client="searchClient" index-name="blogs">
<ais-configure
:hitsPerPage="showing"
:restrictSearchableAttributes="searchCategories"
/>
<ais-autocomplete :classNames="{'ais-Autocomplete': 'tools-searchbar'}">
<template slot-scope="{ currentRefinement, indices, refine }">
<vue-autosuggest
:suggestions="indicesToSuggestions(indices)"
@selected="onSelect"
:input-props="{
id: 'tools-searchbar',
onInputChange: refine,
placeholder: 'Search for blogs...',
}"
>
<template slot-scope="{ suggestion }">
<a id="tools-link" target="_blank">
<v-flex>
<v-avatar class="pr-2" size="36">
<img
:src="suggestion.item.image || '/blogs.png'"
alt="/blogs.png"
/>
</v-avatar>
<span id="tool-name">
<ais-highlight
:hit="suggestion.item"
attribute="resolved_title"
v-if="suggestion.item.resolved_title"
/>
</span>
<div id="tool-description">
<ais-highlight
:hit="suggestion.item"
attribute="excerpt"
v-if="suggestion.item.excerpt"
/>
</div>
</v-flex>
</a>
</template>
</vue-autosuggest>
</template>
</ais-autocomplete>
</ais-instant-search>
我的方法是:
methods: {
onSelect(selected) {
if (selected) {
this.query = "";
window.open(selected.item.resolved_url);
}
},
indicesToSuggestions(indices) {
if (this.loggedin) {
return indices.map(({ hits }) => ({ data: hits }));
}
},
第一张图片是点击匹配结果前的图片。
第二张是点击匹配结果后的图片。
点击后显示值为lol
的组件
根据 Vue-autosuggest 文档:
https://github.com/darrenjennings/vue-autosuggest#getsuggestionvalue
使用道具 get-suggestion-value 并将方法绑定到 return 您希望从该建议中获得的任何属性。
<vue-autosuggest
v-model="query"
:suggestions="filteredOptions"
@focus="focusMe"
@click="clickHandler"
@input="onInputChange"
@selected="onSelected"
:get-suggestion-value="getSuggestionValue"
:input-props="{id:'autosuggest__input', placeholder:'Do you feel lucky, punk?'}">
<div slot-scope="{suggestion}" style="display: flex; align-items: center;">
<img :style="{ display: 'flex', width: '25px', height: '25px', borderRadius: '15px', marginRight: '10px'}" :src="suggestion.item.avatar" />
<div style="{ display: 'flex', color: 'navyblue'}">{{suggestion.item.name}}</div>
</div>
</vue-autosuggest>
并且,
getSuggestionValue(suggestion) {
return suggestion.item.name;
},