vue3中路由傳參query、params及動(dòng)態(tài)路由傳參詳解
一、query傳參
編程式導(dǎo)航 使用router.push
或者 router.replace
的時(shí)候,改為對(duì)象形式新增query
必須傳入一個(gè)對(duì)象
import { useRouter } from 'vue-router'; ... const router = useRouter() const toDetail = (item: Item) => { router.push({ path: '/info', query: item }) }
接受參數(shù)
使用 useRoute
的 query
<template> <div> <div>ID:{{route.query?.id}}</div> <div>名稱:{{route.query?.name}}</div> <div>價(jià)格:{{route.query?.price}}</div> </div> </template> <script setup lang='ts'> import { useRoute } from 'vue-router'; ... const route = useRoute() </script> <style lang='less' scoped> </style>
二、params傳參
編程式導(dǎo)航 使用router.push
或者 router.replace
的時(shí)候,改為對(duì)象形式并且只能使用name
,path無(wú)效,然后傳入params
import { useRouter } from 'vue-router'; ... const router = useRouter() const toDetail = (item: Item) => { router.push({ name: 'Info', params: item }) }
接受參數(shù)
使用 useRoute
的 params
<template> <div> <div>ID:{{route.params?.id}}</div> <div>名稱:{{route.params?.name}}</div> <div>價(jià)格:{{route.params?.price}}</div> </div> </template> <script setup lang='ts'> import { useRoute } from 'vue-router'; ... const route = useRoute() </script> <style lang='less' scoped> </style>
三、動(dòng)態(tài)傳參
很多時(shí)候,我們需要將給定匹配模式的路由映射到同一個(gè)組件。
例如,我們可能有一個(gè) User 組件,它應(yīng)該對(duì)所有用戶進(jìn)行渲染,但用戶 ID 不同。在 Vue Router 中,我們可以在路徑中使用一個(gè)動(dòng)態(tài)字段來(lái)實(shí)現(xiàn),我們稱之為 路徑參數(shù)
// router.ts import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router"; const routes: Array<RouteRecordRaw> = [{ path: '/', name: 'table', component: () => import('@/view/Table/index.vue') }, { path: '/info/:id', name: 'Info', component: () => import('@/view/Table/info.vue') }, ... ] const router = createRouter({ history: createWebHistory(), routes }) export default router
import { useRouter } from 'vue-router'; ... const router = useRouter() const toDetail = (item: Item) => { router.push({ name: 'Info', params: {id: item.id} }) }
接受參數(shù)
使用 useRoute
的 params
<template> <div> <div>ID:{{ item?.id }}</div> <div>名稱:{{ item?.name }}</div> <div>價(jià)格:{{ item?.price }}</div> </div> </template> <script setup lang='ts'> import { useRoute } from 'vue-router'; import { data } from './data.json' ... const route = useRoute() // 模擬根據(jù)id獲取數(shù)據(jù) const item = data.find(v => v.id === Number(route.params.id)) </script> <style lang='less' scoped> </style>
四、query傳參和params傳參的區(qū)別
- query 傳參配置的是 path,而 params 傳參配置的是name,且在 params中配置 path 無(wú)效
- query傳遞的參數(shù)會(huì)顯示在地址欄中,而params傳參不會(huì)
- query傳參刷新頁(yè)面數(shù)據(jù)不會(huì)消失,而params傳參刷新頁(yè)面數(shù)據(jù)回消失
- params可以使用動(dòng)態(tài)傳參,動(dòng)態(tài)傳參的數(shù)據(jù)會(huì)顯示在地址欄中,且刷新頁(yè)面不會(huì)消失,因此可以使用動(dòng)態(tài)params傳參,根據(jù)動(dòng)態(tài)傳遞參數(shù)在傳遞頁(yè)面獲取數(shù)據(jù),以防頁(yè)面刷新數(shù)據(jù)消失
總結(jié)
到此這篇關(guān)于vue3中路由傳參query、params及動(dòng)態(tài)路由傳參的文章就介紹到這了,更多相關(guān)vue3路由傳參query params內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue全局注冊(cè)與局部注冊(cè)兩種組件注冊(cè)的方式
本文主要介紹了Vue全局注冊(cè)與局部注冊(cè)兩種組件注冊(cè)的方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07.netcore+vue 實(shí)現(xiàn)壓縮文件下載功能
這篇文章主要介紹了.netcore+vue 實(shí)現(xiàn)壓縮文件下載功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09vue實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10vue 設(shè)置proxyTable參數(shù)進(jìn)行代理跨域
這篇文章主要介紹了vue 設(shè)置proxyTable參數(shù)進(jìn)行代理跨域的相關(guān)資料,及代理跨域的概念原理,需要的朋友可以參考下2018-04-04vue+axios實(shí)現(xiàn)登錄攔截的實(shí)例代碼
本篇文章主要介紹了vue+axios實(shí)現(xiàn)登錄攔截的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Vue.js獲取手機(jī)系統(tǒng)型號(hào)、版本、瀏覽器類型的示例代碼
這篇文章主要介紹了vue js獲取手機(jī)系統(tǒng)型號(hào)、版本、瀏覽器類型的示例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05