Vue3的vue-router超詳細使用示例教程
搭建vue3環(huán)境
我們使用vite來搭建vue3環(huán)境(沒有安裝vite需要去安裝vite)
npm create vite routerStudy
在命令行選擇


cd routerStudy npm i npm run dev
環(huán)境搭建成功??!

vue-router入門(寶寶模式)
下載vue-router
npm i vue-router@4
新建以下文件
src/components/File1.vue:
<template>
<div>
這是文件一
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>src/components/File2.vue:
<template>
<div>
這是文件二
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>在src下新建router文件夾
在router文件夾下新建router.ts:
import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'
import File1 from '../components/File1.vue'
import File2 from '../components/File2.vue'
const routes = [
{
path: '/',
component:File1
},
{
path: '/file2',
component:File2
}
]
const router = createRouter({
// history: createWebHistory(),
history:createWebHashHistory(),
routes,
})
export default router;修改src/main.ts
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router/router'
createApp(App).use(router).mount('#app')修改src/components/HelloWorld.vue:
<script setup lang="ts">
</script>
<template>
<router-view/>
<button><router-link to="/">去文件一</router-link></button>
<button><router-link to="/file2">去文件二</router-link></button>
</template>
<style scoped>
</style>點擊按鈕能夠切換成功則使用成功

vue-router基礎(青年模式)
一。動態(tài)路由匹配
1.帶參數(shù)的動態(tài)路由匹配
當我們需要對每個用戶加載同一個組件,但用戶id不同。我們就需要在vue-router種使用一個動態(tài)字段來實現(xiàn),再通過$routr.params來獲取值:

我們用具體實例來實現(xiàn)一下:
(1)修改src/router/router.ts:
import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'
import File1 from '../components/File1.vue'
import File2 from '../components/File2.vue'
const routes = [
{
path: '/',
component:File1
},
{
path: '/file2/:username/abc/:userid', //注意看這個
component:File2
}
]
const router = createRouter({
history: createWebHistory(),
// history:createWebHashHistory(),
routes,
})
export default router;(2)修改組件HelloWorld.vue:

(3) 修改組件File2.vue:
<template>
<div>
這是文件二
</div>
</template>
<script setup lang="ts">
import {getCurrentInstance,onMounted } from 'vue'
const instance = getCurrentInstance()
if (instance != null) {
const _this = instance.appContext.config.globalProperties //vue3獲取當前this
onMounted(() => {
console.log(_this.$route.params)
})
}
</script>
<style scoped>
</style>(4)點擊去文件二按鈕

(5)查看控制臺

2.捕獲所有路由或404 Not Found路由
當用戶在導航欄亂輸一通后,路由表中沒有對應的路由,這時候,就需要將用戶轉(zhuǎn)去404頁面。那么
我們該如何處理呢?
(1)修改router/router.ts:
import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'
import File1 from '../components/File1.vue'
import File2 from '../components/File2.vue'
import NotFound from '../components/NotFound.vue'
import UserGeneric from '../components/UserGeneric.vue'
const routes = [
{
path: '/',
component:File1
},
{
path: '/file2/:username/abc/:userid',
component:File2
},
// 將匹配所有內(nèi)容并將其放在 `$route.params.pathMatch` 下
{
path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound
},
// 將匹配以 `/user-` 開頭的所有內(nèi)容,并將其放在 `$route.params.afterUser` 下
{
path: '/user-:afterUser(.*)', component: UserGeneric
},
]
const router = createRouter({
history: createWebHistory(),
// history:createWebHashHistory(),
routes,
})
export default router;
(2)新建組件NotFound.vue:
<template>
<div>
糟糕!頁面沒有找到。。。嗚嗚嗚
</div>
</template>
<script setup lang="ts">
import {getCurrentInstance,onMounted } from 'vue'
const instance = getCurrentInstance()
if (instance != null) {
const _this = instance.appContext.config.globalProperties //vue3獲取當前this
onMounted(() => {
console.log(_this.$route.params)
})
}
</script>
<style scoped>
</style>(3)修改HelloWorld.vue

(4)點擊去404頁面按鈕(或者在地址欄亂寫一通)


(5)出現(xiàn)404頁面,說明運行成功!!!
二。嵌套路由
路由是可以嵌套的。例如:

嵌套的理解挺簡單的,我就不多叭叭了,直接上代碼,看完就懂了。
(1)新建組件Children1.vue:
<template>
<div>
我是孩子1
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>(2)新建組件Children2.vue:
<template>
<div>
我是孩子2
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>
<template>
<div>
我是孩子2
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>(3)修改router/router.ts:
import { createRouter,createWebHistory,createWebHashHistory } from 'vue-router'
import File1 from '../components/File1.vue'
import File2 from '../components/File2.vue'
import NotFound from '../components/NotFound.vue'
import UserGeneric from '../components/UserGeneric.vue'
import Children1 from '../components/Children1.vue'
import Children2 from '../components/Children2.vue'
const routes = [
{
path: '/',
component: File1,
},
{
path: '/file2',
component: File2,
children: [ //使用嵌套路由
{
path: 'children1',
component:Children1
},
{
path: 'children2',
component:Children2
},
]
},
{
path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound
},
{
path: '/user-:afterUser(.*)', component: UserGeneric
},
]
const router = createRouter({
history: createWebHistory(),
// history:createWebHashHistory(),
routes,
})
export default router;
(4)修改組件HelloWorld.vue:

(5)修改組件File2.vue:
<template>
<div>
這是文件二
<div>
我是文件二里的內(nèi)容
<router-view/>
<button><router-link to="/file2/children1">找孩子1</router-link></button>
<button><router-link to="/file2/children2">找孩子2</router-link></button>
</div>
</div>
</template>
<script setup lang="ts">
</script>
<style scoped>
</style>
(6)先點去文件二,再點擊找孩子1按鈕,出現(xiàn)即成功?。?/p>

三。編程式導航
除了使用/< router-link/> 創(chuàng)建 a 標簽來定義導航鏈接,我們還可以借助 router 的實例方法,通過編寫代碼來實現(xiàn)。
1.router.push()方法的使用
(1)修改組件NotFound.vue:
<template>
<div>
糟糕!頁面沒有找到。。。嗚嗚嗚
</div>
</template>
<script setup lang="ts">
import {getCurrentInstance,onMounted } from 'vue'
const instance = getCurrentInstance()
if (instance != null) {
const _this = instance.appContext.config.globalProperties //vue3獲取當前this
// 1.字符串路徑
_this.$router.push('/file2/children2')
// 2.帶有路徑的對象
// _this.$router.push({path:'/file2/children2'})
// 3.命名的路由,并加上參數(shù),讓路由建立 url
// _this.$router.push({name:'file2',params:{username:'children2'}})
// 4.帶查詢參數(shù),結果是 /register?plan=private
// _this.$router.push({ path: '/file2/children2', query: {userid:'123'} })
onMounted(() => {
console.log(_this.$route.params)
})
}
</script>
<style scoped>
</style>
(2)再點“去404頁面”,發(fā)現(xiàn)沒有去404頁面了,說明編程式導航成功??!

2.router.replace()方法的使用
它的作用類似于 router.push,唯一不同的是,它在導航時不會向 history 添加新記錄,正如它的名字所暗示的那樣——它取代了當前的條目。
修改組件NotFound.vue:
<template>
<div>
糟糕!頁面沒有找到。。。嗚嗚嗚
</div>
</template>
<script setup lang="ts">
import {getCurrentInstance,onMounted } from 'vue'
const instance = getCurrentInstance()
if (instance != null) {
const _this = instance.appContext.config.globalProperties //vue3獲取當前this
// 一。router.push的使用:
// 1.字符串路徑
// _this.$router.push('/file2/children2')
// 2.帶有路徑的對象
// _this.$router.push({path:'/file2/children2'})
// 3.命名的路由,并加上參數(shù),讓路由建立 url
// _this.$router.push({name:'file2',params:{username:'children2'}})
// 4.帶查詢參數(shù),結果是 /register?plan=private
// _this.$router.push({ path: '/file2/children2', query: {userid:'123'} })
// 二。router.replace的使用:
_this.$router.replace('/file2/children1')
onMounted(() => {
console.log(_this.$route.params)
})
}
</script>
<style scoped>
</style>
3.router.go()方法的使用
修改組件NotFound.vue:
<template>
<div>
糟糕!頁面沒有找到。。。嗚嗚嗚
</div>
</template>
<script setup lang="ts">
import {getCurrentInstance,onMounted } from 'vue'
const instance = getCurrentInstance()
if (instance != null) {
const _this = instance.appContext.config.globalProperties //vue3獲取當前this
// 一。router.push的使用:
// 1.字符串路徑
// _this.$router.push('/file2/children2')
// 2.帶有路徑的對象
// _this.$router.push({path:'/file2/children2'})
// 3.命名的路由,并加上參數(shù),讓路由建立 url
// _this.$router.push({name:'file2',params:{username:'children2'}})
// 4.帶查詢參數(shù),結果是 /register?plan=private
// _this.$router.push({ path: '/file2/children2', query: {userid:'123'} })
// 二。router.replace的使用:
// _this.$router.replace('/file2/children1')
// 三。router.go的使用:
_this.$router.go(-1) //相當于點擊回退一次
onMounted(() => {
console.log(_this.$route.params)
})
}
</script>
<style scoped>
</style>
到此這篇關于Vue3的vue-router超詳細使用的文章就介紹到這了,更多相關Vue3的vue-router使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mpvue實現(xiàn)左側導航與右側內(nèi)容的聯(lián)動
這篇文章主要為大家詳細介紹了mpvue實現(xiàn)左側導航與右側內(nèi)容的聯(lián)動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
使用vant-picker實現(xiàn)自定義內(nèi)容,根據(jù)內(nèi)容添加圖標
這篇文章主要介紹了使用vant-picker實現(xiàn)自定義內(nèi)容,根據(jù)內(nèi)容添加圖標,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
vue實現(xiàn)微信分享朋友圈,發(fā)送朋友的示例講解
下面小編就為大家分享一篇vue實現(xiàn)微信分享朋友圈,發(fā)送朋友的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
vue學習之mintui picker選擇器實現(xiàn)省市二級聯(lián)動示例
本篇文章主要介紹了vue學習之mintui picker選擇器實現(xiàn)省市二級聯(lián)動示例,非常具有實用價值,需要的朋友可以參考下2017-10-10

