vue3中,我想监听route.query.index
,通过router.push
添加query参数后,第一次进入组件后,获取到的route.query.index
是undefined。
本想着应该是因为router.push
是异步,导致刚加载组件后还没有变化,但是通过watchEffect
监听的话,第一次的变化并没有监听到,第二次router.push
时才会监听到变化,
watchEffect(() => {
console.log(route.query.index)
query.value = route.query.index;
});
之后想通过.then来确保完成后再跳转到组件,但是获取到的依旧是undefined
router.push().then(() => {
nextTick()
})
想请问下这个该如何解决
不是很明白你说的第一次变化是指什么,是进入页面是路径上就带着的参数吗?
另外,这类数据最好是直接用 computed 处理,而不是监听后再赋值。