尝试将路由查询传递给axios
请求,但它是空的.. route.query
在挂载中返回空。route.query
在axios中返回{"filter[city]": "Vilnius" }
then
nextTick
不能解决问题。有任何提示吗?
import { ref, onMounted, nextTick } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
export default {
setup() {
const router = useRouter();
const route = useRoute();
onMounted(() => {
console.log(route.query); // log is {}
fetchApartments();
});
function fetchApartments() {
console.log(route.query); // log is {}
axios.get('/api/apartments').then(response => {
console.log(route.query); // log is { "filter[city]": "Vilnius" }
});
}
}
}
2条答案
按热度按时间js81xvg61#
路线导航是异步的。您需要等待
router.isReady
以使查询可用7lrncoxx2#
更新代码,如下所示:
和内部设置()
这里缺少的部分是计算属性。