国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue3路由配置及路由跳轉(zhuǎn)傳參

這篇具有很好參考價(jià)值的文章主要介紹了vue3路由配置及路由跳轉(zhuǎn)傳參。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1、安裝路由

npm i vue-router

2、編寫需要展示的路由

在src目錄下創(chuàng)建pages文件夾,里面創(chuàng)建兩個(gè)vue文件命名為student.vue,person.vuevue3路由傳參,vue,vue路由,vue.js,javascript,前端

分別編寫兩個(gè)vue文件

student.vue和person.vue

<template>
    學(xué)生
</template>

<script setup>

</script>

<style scoped lang="less">

</style>
<template>
人類
</template>

<script setup>

</script>

<style scoped lang="less">

</style>

3、配置路由

在src目錄下配置router.js文件

import { createRouter,createWebHistory } from "vue-router";
const router=createRouter({
    history:createWebHistory(),
    routes:[
        {
            component:()=>import('../pages/person.vue'),
            name:'person',
            path:'/person'
        },
        {
            component:()=>import('../pages/student.vue'),
            name:'student',
            path:'/student'
        },
        {
            //實(shí)現(xiàn)路由重定向,當(dāng)進(jìn)入網(wǎng)頁(yè)時(shí),路由自動(dòng)跳轉(zhuǎn)到/student路由
            redirect:'/student',
            path:'/'
        }
    ]
})
export default router

3、使用路由

在main.js中使用路由

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

createApp(App).use(router).mount('#app')

在app.vue中進(jìn)行路由展示,使用router-link進(jìn)行路由跳轉(zhuǎn),to代表跳轉(zhuǎn)到哪個(gè)路由

<template>
  <router-view></router-view>
  <hr>
  <div>
    <router-link to="/student">到student路由</router-link>
    <br>
    <router-link to="/person">到person路由</router-link>
  </div>
</template>

<script setup>

</script>
<style scoped>

</style>

效果如下圖所示,點(diǎn)擊(到student路由)或(到person路由)會(huì)進(jìn)行路由跳轉(zhuǎn)

vue3路由傳參,vue,vue路由,vue.js,javascript,前端

?4、編程式路由

聲明式路由通過(guò)router-link進(jìn)行路由跳轉(zhuǎn),編程式路由通過(guò)函數(shù)實(shí)現(xiàn)

修改app.vue,vue3使用的是組合式API,需要引入

要引入useRouter,useRoute,還要

const router=useRouter()

const route=useRoute()

<template>
  <router-view></router-view>
  <hr>
  <div>
    <button @click="toStudent">到student路由</button>
    <br>
    <button @click="toPerson">到person路由</button>
  </div>
</template>

<script setup>
import {useRouter,useRoute} from 'vue-router'
const router=useRouter()
const route=useRoute()
const toStudent=()=>{
  router.push('student')
}
const toPerson=()=>{
  router.push('person')
}
</script>
<style scoped>

</style>

通過(guò)router.push進(jìn)行路由跳轉(zhuǎn)

路由之間用router路由器,當(dāng)前路由使用toute路由

結(jié)果如下圖所示,實(shí)現(xiàn)編程式路由跳轉(zhuǎn)

vue3路由傳參,vue,vue路由,vue.js,javascript,前端

?如果在配置路由時(shí)沒(méi)有設(shè)置別名,需要通過(guò)router.push配置對(duì)象進(jìn)行跳轉(zhuǎn)

const toStudent=()=>{
  router.push({
    path:'/student'
  })
}
const toPerson=()=>{
  router.push({
    path:'/person'
  })
}

5、路由傳參

5、1query參數(shù)傳遞

向student路由傳遞id,name

const toStudent=()=>{
  router.push({
    path:'/student',
    query:{
      id:1,
      name:'張三'
    }
  })
}

student路由接收query參數(shù)

<template>
    學(xué)生組件
    <div>{{data.query}}</div>
</template>

<script setup>
import { reactive } from 'vue';
import {useRouter,useRoute} from 'vue-router'
const route=useRoute()
let data=reactive({
    query: route.query
})
</script>

效果如下圖所示

vue3路由傳參,vue,vue路由,vue.js,javascript,前端

5、2傳遞params參數(shù)?

假設(shè)向person路由傳遞params參數(shù),要在路由配置時(shí)進(jìn)行修改

params傳參需要使用name進(jìn)行指定路由

const toPerson=()=>{
  router.push({
    name:'person',
    params:{
      keyword:2
    }
  })
}

同時(shí)在路由配置需要修改,假設(shè)傳遞的是keyword,

需要在path使用占位符加關(guān)鍵字

?表示可傳可不傳

{
      component:()=>import('../pages/person.vue'),
      name:'person',
      path:'/person/:keyword?'
},

在person.vue中接收params參數(shù)

<template>
    人類組件
    <div>{{data.params.keyword}}</div>
</template>

<script setup>
import { reactive } from 'vue';
import {useRouter,useRoute} from 'vue-router'
const route=useRoute()
let data=reactive({
    params: route.params
})
</script>

效果如下所示

vue3路由傳參,vue,vue路由,vue.js,javascript,前端

6、子路由配置

給student路由添加子組件(stu1,stu2組件)

vue3路由傳參,vue,vue路由,vue.js,javascript,前端?

子組件的path不帶 /??

{
            component:()=>import('../pages/student.vue'),
            name:'student',
            path:'/student',
            children:[
                {
                    path:'stu1',
                    name:'stu1',
                    component:()=>import('../pages/stu1.vue')
                },
                {
                    path:'stu2',
                    name:'stu2',
                    component:()=>import('../pages/stu2.vue')
                },
                {
                    path:'',
                    component:()=>import('../pages/stu1.vue')
                }
            ]
        }

編寫stu1組件

<template>
stu1
</template>

<script setup>

</script>

<style scoped lang="less">

</style>

編寫stu2組件

<template>
stu2
</template>

<script setup>

</script>

<style scoped lang="less">

</style>

?在student組件進(jìn)行子組件展示

<template>
    學(xué)生組件
    <div>{{data.query}}</div>
    子組件展示
    <router-view></router-view>
    <router-link to="/student/stu1">到stu1</router-link>
    <router-link to="/student/stu2">到stu2</router-link>
</template>

<script setup>
import { reactive } from 'vue';
import {useRouter,useRoute} from 'vue-router'
const route=useRoute()
let data=reactive({
    query: route.query
})
</script>

通過(guò)使用router-link進(jìn)行路由跳轉(zhuǎn),也可以通過(guò)編程式路由跳轉(zhuǎn)

to="/student/stu1"? 需要使用完整路徑進(jìn)行跳轉(zhuǎn)

效果展示

vue3路由傳參,vue,vue路由,vue.js,javascript,前端?

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-806575.html

到了這里,關(guān)于vue3路由配置及路由跳轉(zhuǎn)傳參的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包