vue.js - 为什么路由器查询不会在 nuxt 中立即更新?
问题描述
当我触发更新查询的函数时,url 变为 '?test=1' 但查询的控制台日志仍然为空。
methods: {
test() {
this.$router.push({ query: { test: 1 } });
console.log(this.$route.query);
}
}
但是我在 Vue 中使用了相同的方法。查询的控制台日志获取 {test:'1'} 工作正常。如果我想立即在 Nuxt 中得到正确的查询,我该怎么办?我知道有两种方法可以工作
- 手表
- setTimeout(()=>{console.log(this.$route.query)},0);
但我想使用更好的解决方案,还有其他方法可以吗?
解决方案
您可以使用watch
on $route.query
:
methods: {
test() {
this.$router.push({ query: { test: 1 } });
},
},
watch: {
'$route.query'(q) {
console.log(q);
},
},
推荐阅读
- python - 如何不在 Python 复数中打印 j?
- excel - 如果单元格为空,粘贴,否则,转到下一个空白单元格并粘贴
- postgresql - PostgreSQL 中的批量插入非常慢(F#)
- javascript - 按钮的 innerText 在重新加载时不会改变
- python - 键盘中断后无法正常关闭 Asyncio 应用程序
- javascript - 使用来自 JSON 的数据创建和附加多个元素
- java - Hibernate GeneratedValue 无法正常工作
- r - 如何仅在日期轴上必要时显示年份
- javascript - 将纹理应用到四边形的一半
- java - 无法修复不匹配类型:无效为布尔值