Vue3路由配置createRouter、createWebHistory、useRouter和useRoute詳解
前言
Vue3和Vue2基本差不多,只不過需要將createRouter、createWebHistory從vue-router中引入,再進(jìn)行使用。
手動配置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、請注意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手動配置Vue-router環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法
今天小編就為大家分享一篇vue2.0 使用element-ui里的upload組件實(shí)現(xiàn)圖片預(yù)覽效果方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue啟動后請求后端接口報(bào)ERR_EMPTY_RESPONSE錯(cuò)誤的解決
這篇文章主要介紹了vue啟動后請求后端接口報(bào)ERR_EMPTY_RESPONSE錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
使用Vue3+ts?開發(fā)ProTable源碼教程示例
這篇文章主要為大家介紹了使用Vue3+ts?開發(fā)ProTable源碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue?3?中使用?vue-router?進(jìn)行導(dǎo)航與監(jiān)聽路由變化的操作
在Vue3中,通過useRouter和useRoute可以方便地實(shí)現(xiàn)頁面導(dǎo)航和路由變化監(jiān)聽,useRouter允許進(jìn)行頁面跳轉(zhuǎn),而useRoute結(jié)合watch可以根據(jù)路由變化更新組件狀態(tài),這些功能為Vue3應(yīng)用增加了靈活性和響應(yīng)性,使得路由管理更加高效2024-09-09
VUE 配置vue-devtools調(diào)試工具及安裝方法
vue-devtools是一款基于chrome瀏覽器的插件,用于vue應(yīng)用的調(diào)試,這款vue調(diào)試神器可以極大地提高我們的調(diào)試效率。幫助我們快速的調(diào)試開發(fā)vue應(yīng)用。這篇文章主要介紹了VUE 配置vue-devtools調(diào)試工具及安裝步驟 ,需要的朋友可以參考下2018-09-09
vue項(xiàng)目中做編輯功能傳遞數(shù)據(jù)時(shí)遇到問題的解決方法
這篇文章主要介紹了vue項(xiàng)目中做編輯功能傳遞數(shù)據(jù)時(shí)遇到問題的解決方法,vue父組件向子組件傳遞數(shù)據(jù)的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

