<script>
import { useRouter } from 'vue-router'
exprot default {
setup(){
const router = useRouter();
const click = ()=>{
router.push({name:"routerName"})
}
return {
click
}
}
}
</script>
router动态传参
/*router.js*/
{
path:'/shop/id',
name:'shop',
component: () => import(componentsName)
}
/***************************************************/
/*
* id 作为参数
* shop/1
*/
/*shop.js*/
<script>
import { useRoute } from 'vue-router'
export default {
name:"shop",
setup(){
const route = useRoute()
console.log(route.params) // 路由传递的参数
}
}
</script>
Composition API 跳转
<script setup>
import { useRouter } from "vue-router"
let router = useRouter();
const goHome = ()=>{
router.push("/xxx")
}
</script>
<template>
<button @click="goHome">跳转</button>
</template>