Vue路由配置方法詳細(xì)介紹
手動配置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路由時默認(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 router
3、將配置的路由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)時成功獲取了傳參,但不在setup函數(shù)內(nèi)使用useRouter是獲取不了的
到此這篇關(guān)于Vue路由配置方法詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Vue路由配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 3開發(fā)中VueUse強(qiáng)大Hooks庫
VueUse提供了一個豐富且強(qiáng)大的Hooks庫,可以幫助開發(fā)者快速實(shí)現(xiàn)各種功能,提高開發(fā)效率,本文來詳細(xì)的介紹一下,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08vue實(shí)現(xiàn)在線預(yù)覽office文件的示例代碼
本文主要介紹了vue實(shí)現(xiàn)在線預(yù)覽office文件,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10使用vue-router切換頁面時實(shí)現(xiàn)設(shè)置過渡動畫
今天小編就為大家分享一篇使用vue-router切換頁面時實(shí)現(xiàn)設(shè)置過渡動畫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10Vue3集成json-editor-vue3的代碼實(shí)現(xiàn)
這篇文章主要介紹了Vue3集成json-editor-vue3的代碼實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11