欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Vue3如何獲取地址欄參數(shù)

本篇內(nèi)容介紹了“Vue3如何獲取地址欄參數(shù)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

陽(yáng)城網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),陽(yáng)城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為陽(yáng)城千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的陽(yáng)城做網(wǎng)站的公司定做!

Vue3 獲取地址欄參數(shù)有兩個(gè)方式:查詢(xún)參數(shù)和路徑參數(shù)。

Vue3獲取地址欄參數(shù)是從路由router中獲取,查詢(xún)參數(shù)和路徑參數(shù)獲取方式不一樣。

一、查詢(xún)參數(shù)

比如地址 http://127.0.0.1:5173/?code=123123,
我們要獲取code參數(shù)可以路由router獲取,注意是route.query

首先需要在router/index.js中定義好路由

import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/home.vue')
    },
  ]
})

export default router

然后就可以在組件中通過(guò)useRouter獲取query參數(shù)了

<script setup>
import {useRouter} from 'vue-router'

const { currentRoute } = useRouter();
const route = currentRoute.value;

onMounted(()=>{
  let code=route.query.code
  console.log('code', code)
})

</script>

二、路徑參數(shù)

路徑參數(shù)指的是參數(shù)是拼接在地址欄中的。
比如地址 http://127.0.0.1:5173/123123
最后的123123就是參數(shù)。

這種參數(shù)首先要定義要路由,在路由中對(duì)參數(shù)進(jìn)行命名,下面代碼中:code就是命名一個(gè)路徑參數(shù)code

首先需要在router/index.js中定義好路由以及路徑參數(shù)

import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/:code',
      name: 'home',
      component: () => import('../views/home.vue')
    },
  ]
})

export default router

接著就可以在home.vue組件中通過(guò)路由useRouter得到參數(shù),注意是route.params

<script setup>
import {useRouter} from 'vue-router'

const { currentRoute } = useRouter();
const route = currentRoute.value;

onMounted(()=>{
  let code=route.params.code
  console.log('code', code)
})

</script>

三、注意點(diǎn)

入口頁(yè)面App.vue必須定義好router-view標(biāo)簽,不能圖簡(jiǎn)單將上面定義的home組件直接引入到App.vue中,如果直接引入走的就不是路由了,因而通過(guò)useRouter也無(wú)法獲取到路由參數(shù)了

如下錯(cuò)誤示例:

<template>
  <div id="app">
    <home></home>
  </div>

</template>

<script setup>
import home from './views/home.vue';
</script>

正確應(yīng)該是使用router-view標(biāo)簽

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
<script setup>

</script>

“Vue3如何獲取地址欄參數(shù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章標(biāo)題:Vue3如何獲取地址欄參數(shù)
標(biāo)題網(wǎng)址:http://www.chinadenli.net/article38/iphjsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司網(wǎng)站策劃域名注冊(cè)營(yíng)銷(xiāo)型網(wǎng)站建設(shè)企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)