<template>
<div>
<button @click="$router.push({ name: 'wallpaper', query: { search: 'mountains' } })">
look for mountains
</button>
<button @click="$router.push({ name: 'wallpaper', query: { search: 'nuxt' } })">
look for nuxt
</button>
<button @click="$router.push({ name: 'index' })">
Go back to the homepage
</button>
</div>
</template>
<script>
export default {
watch: {
async $route(to, from) {
if (to.name !== 'wallpaper') return // if you're going to somewhere else than `wallpaper`
// the `return` will end the execution and not go further
console.log('fetch logic here in case you stay on the wallpaper route name')
await this.fetch()
},
},
}
</script>
2条答案
按热度按时间xmakbtuz1#
这种代码应该可以完成这项工作。
字符串
当您更改查询时,您也可以再次调用
fetch()
方法。3bygqnnd2#
对于Nuxt2,您可以使用
watchQuery
仅监视查询更新。https://nuxtjs.org/docs/2.x/components-glossary/pages-watchquery
**示例:**只要查询参数发生变化,就会立即执行Fetch,但路由发生变化时不会执行Fetch:
字符串