最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3的路由傳參方法超全匯總

 更新時(shí)間:2023年04月23日 16:20:28   作者:小輝吖~  
vue路由傳參的使用場(chǎng)景一般都是應(yīng)用在父路由跳轉(zhuǎn)到子路由時(shí),攜帶參數(shù)跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于Vue3路由傳參方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

下面方法刷新參數(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Vue.js中的v-on(事件處理)

    淺談Vue.js中的v-on(事件處理)

    本篇文章主要介紹了Vue.js中的v-on(事件處理),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • Vue常用的全選/反選的示例代碼

    Vue常用的全選/反選的示例代碼

    這篇文章主要介紹了Vue常用的全選/反選的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • vue實(shí)現(xiàn)移動(dòng)端懸浮窗效果

    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)題

    這篇文章主要介紹了解決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)題

    這篇文章主要介紹了解決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)簽的兩種方法

    這篇文章主要介紹了Vue.js項(xiàng)目中管理每個(gè)頁(yè)面的頭部標(biāo)簽的兩種方法,需要的朋友可以參考下
    2018-06-06
  • Vue組件間數(shù)據(jù)傳遞的方式(3種)

    Vue組件間數(shù)據(jù)傳遞的方式(3種)

    這篇文章主要介紹了Vue組件間數(shù)據(jù)傳遞的方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Vue中訪問(wèn)指定鏈接并解析頁(yè)面內(nèi)容的完整指南

    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ú)限加載思路和步驟詳解

    這篇文章主要介紹了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將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

最新評(píng)論

兴和县| 塘沽区| 凤庆县| 新兴县| 长宁县| 连云港市| 黄浦区| 万源市| 新安县| 克山县| 衡阳市| 富裕县| 平顺县| 油尖旺区| 赤城县| 临猗县| 天镇县| 鄂州市| 济源市| 大洼县| 冀州市| 泰顺县| 岫岩| 镇江市| 叙永县| 象州县| 古交市| 天长市| 达日县| 遂平县| 望谟县| 太谷县| 德庆县| 天祝| 白玉县| 阿坝县| 鄱阳县| 营山县| 闵行区| 扬中市| 伊金霍洛旗|