您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“Vue3的路由传参方法有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vue3的路由传参方法有哪些”吧!
下面方法刷新参数都不会丢失
路由配置(需要配置成动态路由形式,原先的直接传参不能用了)
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, }, { path: '/about/:id', name: 'about', component: () => import('../views/AboutView.vue'), }, ], }) export default router
组件A
<script> import { useRouter } from 'vue-router' export default { name: 'Home', setup() { const router = useRouter() const toAbout = () => { router.push({ name: 'about', params: { id: 100, }, }) } return { toAbout, } }, } </script> <template> <main> <button @click="toAbout">to About</button> </main> </template>
组件B
<script> import { useRoute } from 'vue-router' export default { name: 'about', setup() { const route = useRoute() console.log('99999999', route.params) }, } </script> <template> <div class="about"> <h2>about</h2> </div> </template>
路由配置(普通形式即可,query会将参数?拼接到路径上)
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, }, { path: '/about', name: 'about', component: () => import('../views/AboutView.vue'), }, ], }) export default router
组件A
<script> import { useRouter } from 'vue-router' export default { name: 'Home', setup() { const router = useRouter() const toAbout = () => { router.push({ name: 'about', query: { id: 100, }, }) } return { toAbout, } }, } </script> <template> <main> <button @click="toAbout">to About</button> </main> </template>
组件B
<script> import { useRoute } from 'vue-router' export default { name: 'about', setup() { const route = useRoute() console.log('99999999', route.query) }, } </script> <template> <div class="about"> <h2>about</h2> </div> </template>
路由配置(普通形式即可,query会将参数?拼接到路径上)
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, }, { path: '/about', name: 'about', component: () => import('../views/AboutView.vue'), }, ], }) export default router
组件A
<script> import { useRouter } from 'vue-router' export default { name: 'Home', setup() { const router = useRouter() const toAbout = () => { router.push({ path: '/about', query: { id: 100, }, }) } return { toAbout, } }, } </script> <template> <main> <button @click="toAbout">to About</button> </main> </template>
组件B
<script> import { useRoute } from 'vue-router' export default { name: 'about', setup() { const route = useRoute() console.log('99999999', route.query) }, } </script> <template> <div class="about"> <h2>about</h2> </div> </template>
路由配置
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, }, { path: '/about', name: 'about', component: () => import('../views/AboutView.vue'), }, ], }) export default router
组件A
<script> import { useRouter } from 'vue-router' export default { name: 'Home', setup() { const router = useRouter() const toAbout = () => { router.push('/about?id=100') } return { toAbout, } }, } </script> <template> <main> <button @click="toAbout">to About</button> </main> </template>
组件B
<script> import { useRoute } from 'vue-router' export default { name: 'about', setup() { const route = useRoute() console.log('99999999', route.query) }, } </script> <template> <div class="about"> <h2>about</h2> </div> </template>
路由配置
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView, }, { path: '/about/:id', name: 'about', component: () => import('../views/AboutView.vue'), }, ], }) export default router
组件A
<script> import { useRouter } from 'vue-router' export default { name: 'Home', setup() { const router = useRouter() const toAbout = () => { router.push('/about/100') } return { toAbout, } }, } </script> <template> <main> <button @click="toAbout">to About</button> </main> </template>
组件B
<script> import { useRoute } from 'vue-router' export default { name: 'about', setup() { const route = useRoute() console.log('99999999', route.params) }, } </script> <template> <div class="about"> <h2>about</h2> </div> </template>
感谢各位的阅读,以上就是“Vue3的路由传参方法有哪些”的内容了,经过本文的学习后,相信大家对Vue3的路由传参方法有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。