Vue3的路由傳參方法超全匯總
下面方法刷新參數(shù)都不會(huì)丟失
1. name + params
路由配置(需要配置成動(dòng)態(tài)路由形式,原先的直接傳參不能用了)
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">
<h1>about</h1>
</div>
</template>2. name + query
路由配置(普通形式即可,query會(huì)將參數(shù)?拼接到路徑上)
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">
<h1>about</h1>
</div>
</template>3. path + query
路由配置(普通形式即可,query會(huì)將參數(shù)?拼接到路徑上)
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">
<h1>about</h1>
</div>
</template>4. 路徑字符串?拼接參數(shù)
路由配置
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">
<h1>about</h1>
</div>
</template>5. 路徑字符串 / 拼接參數(shù)
路由配置
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">
<h1>about</h1>
</div>
</template>總結(jié)
到此這篇關(guān)于Vue3路由傳參方法的文章就介紹到這了,更多相關(guān)Vue3路由傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 3種vue路由傳參的基本模式
- vue動(dòng)態(tài)路由配置及路由傳參的方式
- vue路由對(duì)不同界面進(jìn)行傳參及跳轉(zhuǎn)的總結(jié)
- vue路由跳轉(zhuǎn)傳參數(shù)的方法
- vue路由傳參的基本實(shí)現(xiàn)方式小結(jié)【三種方式】
- 如何處理vue router 路由傳參刷新頁(yè)面參數(shù)丟失
- vue3中路由傳參query、params及動(dòng)態(tài)路由傳參詳解
- Vue路由跳轉(zhuǎn)傳參或打開(kāi)新頁(yè)面跳轉(zhuǎn)的方法總結(jié)
- vue中路由傳參6種方式總結(jié)
- vue路由三種傳參方式詳細(xì)講解
相關(guān)文章
vue實(shí)現(xiàn)移動(dòng)端懸浮窗效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動(dòng)端懸浮窗效果,vuejs實(shí)現(xiàn)div拖拽移動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
解決vue中axios設(shè)置超時(shí)(超過(guò)5分鐘)沒(méi)反應(yīng)的問(wèn)題
這篇文章主要介紹了解決vue中axios設(shè)置超時(shí)(超過(guò)5分鐘)沒(méi)反應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
解決vue中reader.onload讀取文件的異步問(wèn)題
這篇文章主要介紹了解決vue中reader.onload讀取文件的異步問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue.js項(xiàng)目中管理每個(gè)頁(yè)面的頭部標(biāo)簽的兩種方法
這篇文章主要介紹了Vue.js項(xiàng)目中管理每個(gè)頁(yè)面的頭部標(biāo)簽的兩種方法,需要的朋友可以參考下2018-06-06
Vue中訪問(wèn)指定鏈接并解析頁(yè)面內(nèi)容的完整指南
在現(xiàn)代Web開(kāi)發(fā)中,經(jīng)常需要從其他網(wǎng)頁(yè)獲取并解析內(nèi)容,本文將詳細(xì)介紹如何在Vue項(xiàng)目中實(shí)現(xiàn)這一功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
vue實(shí)現(xiàn)商品列表的無(wú)限加載思路和步驟詳解
這篇文章主要介紹了vue實(shí)現(xiàn)商品列表的無(wú)限加載思路和步驟詳解,基礎(chǔ)思路是觸底條件滿足之后 page++,拉取下一頁(yè)數(shù)據(jù),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下,2024-06-06
Vue將el-table導(dǎo)出為excel文件的實(shí)現(xiàn)方法
在 Vue + Element UI 中,el-table 數(shù)據(jù)導(dǎo)出 Excel 文件,可以使用 xlsx(SheetJS)庫(kù)進(jìn)行處理,以下是詳細(xì)的實(shí)現(xiàn)方法,包括安裝依賴(lài)、代碼示例和優(yōu)化建議,需要的朋友可以參考下2025-02-02

