vue中router-view使用教程詳解
一、介紹
router-view組件作為vue最核心的路由管理組件,在項(xiàng)目中作為路由管理經(jīng)常被使用到。vue項(xiàng)目最核心的App.vue文件中即是通過(guò)router-view進(jìn)行路由管理。
<template> <div id="app"> <router-view></router-view> </div> </template>
我們?cè)谧约壕S護(hù)項(xiàng)目的時(shí)候,也可以使用router-view組件進(jìn)行路由管理,對(duì)于頁(yè)面局部刷新的場(chǎng)景,該組件能發(fā)揮關(guān)鍵作用;以下介紹vue2項(xiàng)目使用router-view進(jìn)行項(xiàng)目路由試圖管理詳解
二、使用方法
我們通過(guò)具體場(chǎng)景來(lái)介紹router-view組件用法:
1 實(shí)現(xiàn)效果
通過(guò)切換底部導(dǎo)航欄進(jìn)行頁(yè)面內(nèi)容區(qū)域切換:
實(shí)現(xiàn)的功能是:點(diǎn)擊消息、聯(lián)系人、動(dòng)態(tài);頁(yè)面內(nèi)容進(jìn)行切換;頁(yè)面標(biāo)題以及底部導(dǎo)航欄不變;
2 代碼
最關(guān)鍵的是router.js配置:
{ path: "/routerViewPractice", name: "routerViewPractice", component: () => import("@/views/routerView/index.vue"), redirect: '/messagePage',//頁(yè)面默認(rèn)加載的路由 children: [ { path: "/messagePage", name: "messagePage", component: () => import("@/views/routerView/childPages/messagePage.vue") }, { path: "/contactPage", name: "contactPage", component: () => import("@/views/routerView/childPages/contactPage.vue") }, { path: "/dynamicPage", name: "dynamicPage", component: () => import("@/views/routerView/childPages/dynamicPage.vue") } ] }
文件說(shuō)明:
- routerViewPractice:父路由path;
- redirect:頁(yè)面router-view組件默認(rèn)加載的路由;
- children:用于父頁(yè)面進(jìn)行切換的子路由;
vue父頁(yè)面代碼:
<template> <div> <title-bar :title="title" @goBack="goback"></title-bar> <router-view></router-view> <BottomBar @handleMsg='handleMsg' @lookContact='lookContact' @readDynamic='readDynamic' ></BottomBar> </div> </template> <script> import TitleBar from "@/components/TitleBar"; import BottomBar from "@/components/BottomBar"; export default { name: "", components: { TitleBar, BottomBar }, data() { return { title: "路由視圖", }; }, methods: { // 返回方法 goback() { // this.$emit("GoBack"); }, handleMsg() { this.$router.push({path: '/messagePage'}) }, lookContact() { this.$router.push({path: '/contactPage'}) }, readDynamic() { this.$router.push({path: '/dynamicPage'}) } } }; </script> <style scoped> #page-title { width: 100%; background-color: #fff; display: flex; justify-content: center; } .backImg { width: 20px; } </style>
使用this.$router.push進(jìn)行頁(yè)面上router-view組件的路由替換;實(shí)現(xiàn)點(diǎn)擊底部導(dǎo)航欄頁(yè)面切換功能;
到此這篇關(guān)于vue中router-view使用教程詳解的文章就介紹到這了,更多相關(guān)vue router-view內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3利用組合式函數(shù)和Shared Worker實(shí)現(xiàn)后臺(tái)分片上傳
這篇文章主要為大家詳細(xì)介紹了Vue3如何利用組合式函數(shù)和Shared Worker實(shí)現(xiàn)后臺(tái)分片上傳(帶哈希計(jì)算),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10VUE 解決mode為history頁(yè)面為空白的問(wèn)題
今天小編就為大家分享一篇VUE 解決mode為history頁(yè)面為空白的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vuex中store.commit和store.dispatch的區(qū)別及使用方法
這篇文章主要介紹了vuex中store.commit和store.dispatch的區(qū)別及使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Vue寫一個(gè)簡(jiǎn)單的倒計(jì)時(shí)按鈕功能
這篇文章主要介紹了基于Vue寫一個(gè)簡(jiǎn)單的倒計(jì)時(shí)按鈕功能,在項(xiàng)目開(kāi)發(fā)的過(guò)程,經(jīng)常會(huì)遇到發(fā)送驗(yàn)證碼,點(diǎn)擊之后有60秒倒計(jì)時(shí)的按鈕,今天小編就給大家分享實(shí)例代碼,需要的朋友參考下吧2018-04-04使用vue-print-nb打印el-table問(wèn)題總結(jié)
這篇文章主要介紹了使用vue-print-nb打印el-table問(wèn)題總結(jié),通過(guò)實(shí)例代碼介紹了vue-print-nb 打印功能,本文結(jié)合實(shí)例代碼講解的非常詳細(xì),感興趣的朋友一起看看吧2024-01-01VUE+ElementUI下載文件的幾種方式(小結(jié))
本文主要介紹了VUE+ElementUI下載文件的幾種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01