引入:
import { useRouter } from 'vue-router'
setup使用 :
const router = useRouter()
watch(() => router.currentRoute.value.path,(toPath) => {
//要执行的方法
},{immediate: true,deep: true})
{immediate: true,deep: true} 要加上,不加的首页不会触发要执行的方法;
示例:首次进入执行定义的方法,当进入home页面也要执行定义的方法,如下操作:
watch(() => router.currentRoute.value.path,(toPath) => {
//要执行的方法
console.log(toPath,"toPath");
if(toPath=="/home"){
getLatestTrendst()
}
},{immediate: true,deep: true})