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

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

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

下面方法刷新參數(shù)都不會丟失

1. name + params

路由配置(需要配置成動態(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會將參數(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會將參數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

    vue實(shí)現(xiàn)移動端懸浮窗效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動端懸浮窗效果,vuejs實(shí)現(xiàn)div拖拽移動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 解決vue中axios設(shè)置超時(超過5分鐘)沒反應(yīng)的問題

    解決vue中axios設(shè)置超時(超過5分鐘)沒反應(yīng)的問題

    這篇文章主要介紹了解決vue中axios設(shè)置超時(超過5分鐘)沒反應(yīng)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 解決vue中reader.onload讀取文件的異步問題

    解決vue中reader.onload讀取文件的異步問題

    這篇文章主要介紹了解決vue中reader.onload讀取文件的異步問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue.js項目中管理每個頁面的頭部標(biāo)簽的兩種方法

    Vue.js項目中管理每個頁面的頭部標(biāo)簽的兩種方法

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

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

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

    Vue中訪問指定鏈接并解析頁面內(nèi)容的完整指南

    在現(xiàn)代Web開發(fā)中,經(jīng)常需要從其他網(wǎng)頁獲取并解析內(nèi)容,本文將詳細(xì)介紹如何在Vue項目中實(shí)現(xiàn)這一功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • vue實(shí)現(xiàn)商品列表的無限加載思路和步驟詳解

    vue實(shí)現(xiàn)商品列表的無限加載思路和步驟詳解

    這篇文章主要介紹了vue實(shí)現(xiàn)商品列表的無限加載思路和步驟詳解,基礎(chǔ)思路是觸底條件滿足之后 page++,拉取下一頁數(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)庫進(jìn)行處理,以下是詳細(xì)的實(shí)現(xiàn)方法,包括安裝依賴、代碼示例和優(yōu)化建議,需要的朋友可以參考下
    2025-02-02

最新評論

彰化县| 黄浦区| 韶关市| 都匀市| 大渡口区| 上犹县| 堆龙德庆县| 珲春市| 凤山市| 东源县| 阿拉善右旗| 临漳县| 天镇县| 盐源县| 永平县| 瓮安县| 洛浦县| 格尔木市| 娱乐| 阿鲁科尔沁旗| 凤翔县| 清徐县| 宕昌县| 扶余县| 岳普湖县| 炎陵县| 双牌县| 宁陵县| 茌平县| 泗洪县| 张掖市| 隆德县| 新沂市| 南和县| 蕉岭县| 公安县| 镇康县| 桐柏县| 万山特区| 江阴市| 广昌县|