三种可选方案
1. 给router-view
添加key属性
<router-view :key="$route.fullPath"/>
2. 使用watch监听id变化重新拉取接口
watch(
()=>{ return route.params.id },
()=>{ loadCategoryList() } // 在id变化的时候重新使用最新的路由id拉取最新数据
)
3. 使用onBeforeRouteUpdate
钩子函数 (推荐)
async function loadCategoryList (id) {
const res = await findTopCategory(id)
categoryData.value = res.result
breadName.value = res.result.name
}
onMounted(() => {
loadCategoryList(route.params.id)
})
// 在路由跳转之后更新之前自动执行
// beforeEach((to,from,next)=>{})
onBeforeRouteUpdate((to) => {
// to指代的是目标路由对象 to拿到最新的路由参数id
// 使用最新id获取数据
loadCategoryList(to.params.id)
})