vue3 router传参
vue3 router传参
A页面点击按钮后携带参数跳转到B页面
原理
- 导入router
import { useRouter } from "vue-router";
- A页面传参
router.push({})
- B页面接收
route.params.num;
demo
route.js
1 2
| { path: '/A', name:'A',component: () => import('pages/A.vueB') }, { path: '/B', name:'B',component: () => import('pages/B.vue') }
|
A页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <template> <button @click="go">跳转</button> </template> <script> import { useRouter } from "vue-router"; export default ({ setup(props, ctx) { let router = useRouter(); const go=()=>{ router.push({ name:"B", params:{ num:1 }
}) } return{ go, } } }) </script>
|
B页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <template> {{ num }} </template> <script> import { useRoute } from "vue-router";
export default { setup(props, ctx) { let route = useRoute(); const num = route.params.num; console.log(num); return { num, }; }, }; </script>
|