Vue3路由配置createRouter、createWebHistory、useRouter和useRoute詳解
前言
Vue3和Vue2基本差不多,只不過需要將createRouter、createWebHistory從vue-router中引入,再進(jìn)行使用。
手動(dòng)配置Vue-router環(huán)境:
1、下載包: npm i vue-router --save或者 npm i vue-router --S 或者用cdn引入
2、創(chuàng)建路由的js文件(路由、子路由、重定向、開啟history模式)
createRouter、createWebHistory
//路由文件 import { createRouter, createWebHistory } from 'vue-router' //將createRouter、createWebHistory引入vue const routes = [ { path: '/', //配置默認(rèn)路由 name: 'home', //路由名 component: () => import("../views/home.vue"), //引入該路由使用的組件 }, { path: '/a', name: 'a', component: () => import('../views/a.vue'), redirect: '/a/son1', children:[ //配置子路由 { path: '/a/son1', //子路由路徑前邊必須寫父路由路徑 name: 'ason1', component: ()=>import("../views/a-son1.vue") } ] }, { path: '/b', name: 'b', component: () => import('../views/b.vue'), redirect: '/b/son1', //重定向,進(jìn)入/b路由時(shí)默認(rèn)進(jìn)入/b/son1 children:[ //配置子路由 { path: '/b/son1', //子路由路徑前邊必須寫父路由路徑 name: 'bson1', component: ()=>import("../views/b-son1.vue") } ] } ] const router = createRouter({ //設(shè)置為history模式 history: createWebHistory(), routes }) export default router3、將配置的路由js文件引入到main.js中
import { createApp } from 'vue' import App from './App.vue' const app=createApp(App) import router from "./router/index.js" //引入配置路由文件 app.use(router)//記得在mount之前調(diào)用 app.mount('#app')4、界面中使用router-view標(biāo)簽顯示路由
組件內(nèi)部跳轉(zhuǎn)路由、傳參useRouter,useRoute
vue3中,在組件內(nèi)部跳轉(zhuǎn)路由 需要使用useRouter,useRoute方法
useRoute相當(dāng)于以前的this.$route 跳轉(zhuǎn)路由
用法:
<template>
<h1>aaa</h1>
<router-view></router-view>
<button @click="fn">從a路由跳轉(zhuǎn)到b路由</button>
</template>
<script setup>
import {useRouter} from "vue-router"
let router=useRouter() //接收useRouter方法,在vue2中是直接使用router即可
let fn=()=>{
router.push({path:"/b",query:{name:"小獅子"}}) //path寫跳轉(zhuǎn)的路由,同樣可以傳參
}
</script>
<style scoped>
h1{
width: 400px;
height:200px;
background-color:deeppink;
}
</style>useRouter相當(dāng)于this.$router 接受傳參(query、params)
注意:1、請(qǐng)注意params只與name(路由文件里配置的路由name)搭配生效(不能使用path)
2、只能在setup函數(shù)內(nèi)使用
用法
<template>
<h2>這是b-son1</h2>
<button @click="fn">lookquery</button>
</template>
<script setup>
import {useRoute} from "vue-router" //引入
let route=useRoute()
console.log(route.query)//如果是params傳參就用route.params接收
let fn=()=>{ //這不是setup函數(shù)內(nèi)部,是取不到傳參的,返回undefined
let route=useRoute()
console.log(route)
}
</script>
<style scoped>
h2{
width: 200px;
height:100px;
background-color:aliceblue;
}
</style>結(jié)合前者代碼進(jìn)行驗(yàn)證,發(fā)現(xiàn)下圖狀況

當(dāng)我們進(jìn)行頁面跳轉(zhuǎn)時(shí)成功獲取了傳參,但不在setup函數(shù)內(nèi)使用useRouter是獲取不了的
總結(jié)
到此這篇關(guān)于Vue3路由配置createRouter、createWebHistory、useRouter和useRoute的文章就介紹到這了,更多相關(guān)Vue3手動(dòng)配置Vue-router環(huán)境內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)雙向綁定
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)雙向綁定,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
vue 使用自定義指令實(shí)現(xiàn)表單校驗(yàn)的方法
今天小編就為大家分享一篇vue 使用自定義指令實(shí)現(xiàn)表單校驗(yàn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取
這篇文章主要介紹了vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue實(shí)現(xiàn)類似Spring官網(wǎng)圖片滑動(dòng)效果方法
這篇文章主要介紹了Vue實(shí)現(xiàn)類似Spring官網(wǎng)圖片滑動(dòng)效果方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
vue項(xiàng)目中如何配置eslint和prettier
這篇文章主要介紹了vue項(xiàng)目中如何配置eslint和prettier問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

