詳解Vue中router-view組件的使用
一、介紹
router-view組件作為vue最核心的路由管理組件,在項(xiàng)目中作為路由管理經(jīng)常被使用到。vue項(xiàng)目最核心的App.vue文件中即是通過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ì)于頁面局部刷新的場景,該組件能發(fā)揮關(guān)鍵作用;
二、使用方法
我們通過具體場景來介紹router-view組件用法:
1 實(shí)現(xiàn)效果
通過切換底部導(dǎo)航欄進(jìn)行頁面內(nèi)容區(qū)域切換:

實(shí)現(xiàn)的功能是:點(diǎn)擊消息、聯(lián)系人、動(dòng)態(tài);頁面內(nèi)容進(jìn)行切換;頁面標(biāo)題以及底部導(dǎo)航欄不變;
2 代碼
最關(guān)鍵的是router.js配置:
{
path: "/routerViewPractice",
name: "routerViewPractice",
component: () => import("@/views/routerView/index.vue"),
redirect: '/messagePage',//頁面默認(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")
}
]
}文件說明:
- routerViewPractice:父路由path;
- redirect:頁面router-view組件默認(rèn)加載的路由;
- children:用于父頁面進(jìn)行切換的子路由;
vue父頁面代碼:
<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)行頁面上router-view組件的路由替換;實(shí)現(xiàn)點(diǎn)擊底部導(dǎo)航欄頁面切換功能;
以上就是詳解Vue中router-view組件的使用的詳細(xì)內(nèi)容,更多關(guān)于Vue router-view的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue中的路由跳轉(zhuǎn)及傳參的多種方法小結(jié)
這篇文章主要介紹了Vue中的路由跳轉(zhuǎn)及傳參的多種方法小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
vue后臺(tái)管理如何配置動(dòng)態(tài)路由菜單
這篇文章主要介紹了vue后臺(tái)管理如何配置動(dòng)態(tài)路由菜單,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue在響應(yīng)頭response中獲取自定義headers操作
這篇文章主要介紹了vue在響應(yīng)頭response中獲取自定義headers操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue實(shí)現(xiàn)codemirror代碼編輯器中的SQL代碼格式化功能
這篇文章主要介紹了vue實(shí)現(xiàn)codemirror代碼編輯器中的SQL代碼格式化功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
vue 實(shí)現(xiàn)滾動(dòng)到底部翻頁效果(pc端)
這篇文章主要介紹了pc端vue 滾動(dòng)到底部翻頁效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
vue如何實(shí)時(shí)往數(shù)組里追加數(shù)據(jù)
這篇文章主要介紹了vue如何實(shí)時(shí)往數(shù)組里追加數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04

