我的应用中包含以下组件:
<ais-menu attribute="productType"></ais-menu>
我想关注此属性的更改,并在更改时对我的页面进行全局更改。
例如:
watch:{
"state.productType": {
deep: true,
handler(state){
console.log("If this text logs, my question is answered!");
}
}
}
是否可以将观察者添加到搜索属性,或者是否可以在属性更改时触发事件?
ais-menu docs显示可以设置为组件的 properties ,但是似乎没有可以监听的事件,或者设置观察者的示例。
类似地,我想知道整个<ais-instant-search>
组件上是否有可用的东西,但是我在文档中找不到任何东西。
This issue指的是'searchStore._results'
属性,但后来的评论者说它不再起作用。
watch: {
'searchStore._results'(val) {
// Emit event
}
}
下面的完整代码:
<template>
<div>
<ais-instant-search :search-client="searchClient" index-name="galeta_products" :routing="routing">
<div class="centered-banner text-center">
<h1 class="h2">{{ taxName }}</h1>
<p>{{ taxDescription }}</p>
</div>
<ais-menu attribute="productType">
<ul
slot-scope="{
items,
canToggleShowMore,
isShowingMore,
toggleShowMore,
refine,
createURL,
}"
>
<li v-for="item in items" :key="item.value" :class="{ 'active' : item.isRefined }">
<a
:href="createURL(item.value)"
@click.prevent="refine(item.value)"
>
{{ item.label }}
</a>
</li>
</ul>
</ais-menu>
</ais-instant-search>
</div>
</template>
<script>
import algoliasearch from 'algoliasearch/lite';
import routing from './SearchRouting.js';
export default {
props: ['taxName', 'taxDescription'],
data() {
return {
liveTaxName: this.taxName,
liveTaxDescription: this.taxDescription,
searchClient: algoliasearch(
'XXXXXXXX',
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
),
routing
};
},
watch:{
// Here is what I want to do
"state.productType": {
deep: true,
handler(state){
this.liveTaxName = TAXONOMIES.find(tax => tax.slug == state.category).name;
this.liveTaxDescription = TAXONOMIES.find(tax => tax.slug == state.category).description;
}
}
}
};
</script>
答案 0 :(得分:0)
一种可能的解决方案是将调用包装为refine
函数。然后,您可以触发任何自定义代码。
<template>
<!-- ... -->
<ais-menu attribute="categories">
<ul slot-scope="{ items, refine }">
<li v-for="item in items" :key="item.value">
<a href="#" @click.prevent="onMenuClick(item.value, refine)">
{{ item.label }}
</a>
</li>
</ul>
</ais-menu>
</template>
<script>
export default {
// ...
methods: {
onMenuClick(value, refine) {
console.log(value);
refine(value);
}
}
};
</script>
您可以在CodeSandbox上找到示例。