vue使用路由router-view的詳細(xì)代碼
前言: router-view 與 NavMenu 導(dǎo)航欄的配合,在 web 應(yīng)用中極為常見。其原理就是采用 SPA(single-page-application) 模式,就是只有一個(gè) Web 頁(yè)面的應(yīng)用,通過(guò) router 來(lái)控制頁(yè)面的刷新和迭代。
提示: 以下示例基于 vue2.6.14 版本,vue-router3.5.2 版本,element-ui2.15.12 版本。
好了,廢話不多說(shuō),我們直接貼上全部代碼
一、創(chuàng)建vue頁(yè)面與js腳本
1、 如下圖,我們?cè)陧?xiàng)目的 src 目錄里,新建 router 文件夾,并創(chuàng)建 index.js 文件。新建 views 目錄,并把我們的 vue 頁(yè)面全部放在這里。
2、 其中每個(gè) index.vue 的內(nèi)容都很簡(jiǎn)單,只有一行文字
<template> <div>view-1</div> </template> <script> /* eslint-disable */ export default { name: 'view-1', mounted() { }, } </script>
3、 另外 package.json 的依賴關(guān)系也一并貼上, 至此可以執(zhí)行 npm install 來(lái)安裝依賴庫(kù)
二、編寫腳本
1、編輯router目錄的index.js文件 (這里只做了二級(jí)目錄的深度)
import Vue from "vue"; import VueRouter from "vue-router"; Vue.use(VueRouter); /* * 我們?cè)陧?xiàng)目中使用router.push或router.replace時(shí),如果在A界面繼續(xù)跳轉(zhuǎn)A界面,就會(huì)拋出異常報(bào)錯(cuò)。 * 這里處理一下捕獲異常的邏輯,就可以避免報(bào)錯(cuò)了。 */ const originPush = VueRouter.prototype.push; VueRouter.prototype.push = function push(location) { return originPush.call(this, location).catch(err => err); } const originReplace = VueRouter.prototype.replace; VueRouter.prototype.replace = function replace(location) { return originReplace.call(this, location).catch(err => err); } const router = new VueRouter({ mode: "history", routes: [ { path: "/view-1", name: "view-1", component: { render: e => e("router-view") }, // 這里就是將children的內(nèi)容渲染到頁(yè)面上 meta: { title: 'view-1', keepAlive: true, icon: "el-icon-document-copy" }, children: [ { path: "/view-1/view-1-1", // 這個(gè)path可以去掉 /view-1,這里只是方便知道其從屬關(guān)系 name: "view-1-1", component: () => import('@/views/view-1-1'), meta: { title: 'view-1-1', keepAlive: true, icon: "el-icon-film" }, }, { path: "/view-1/view-1-2", name: "view-1-2", component: () => import('@/views/view-1-2'), meta: { title: 'view-1-2', keepAlive: true, icon: "el-icon-bank-card" }, } ], }, { path: "/view-2", name: "view-2", component: { render: e => e("router-view") }, meta: { title: 'view-2', keepAlive: true, icon: "el-icon-connection" }, children: [ { path: "/view-2/view-2-1", name: "view-2-1", component: () => import('@/views/view-2-1'), meta: { title: 'view-2-1', keepAlive: true, icon: "el-icon-odometer" }, } ], }, { path: "/view-3", name: "view-3", component: () => import('@/views/view-3'), meta: { title: 'view-3', keepAlive: true, icon: "el-icon-truck" }, }, ] }); export default router;
2、編輯項(xiàng)目的main.js
import Vue from 'vue'; import App from './App.vue'; import router from './router'; // 引入并使用element-ui import * as element from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; import 'element-ui/lib/theme-chalk/display.css'; Vue.config.productionTip = false; Vue.use(element); new Vue({ render: h => h(App), router, // 使用router }).$mount('#app')
3、編輯項(xiàng)目的App.vue
<template> <div id="app"> <el-menu :default-active="this.$route.path" class="el-menu-vertical-demo" router unique-opened background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" > <div v-for="(routeItem, routeIndex) in routes" :key="routeIndex"> <el-submenu v-if="routeItem.children && routeItem.children.length > 0" :index="routeItem.path"> <template slot="title"><i :class="routeItem.meta.icon"></i><span>{{ routeItem.meta.title }}</span></template> <el-menu-item v-for="(subItem, subIndex) in routeItem.children" :index="subItem.path" :key="subIndex" @click="handleShowItem(subItem)" > <template slot="title"><i :class="subItem.meta.icon"></i><span>{{ subItem.meta.title }}</span></template> </el-menu-item> </el-submenu> <el-menu-item v-else :index="routeItem.path" @click="handleShowItem(routeItem)"> <template slot="title"><i :class="routeItem.meta.icon"></i><span>{{ routeItem.meta.title }}</span></template> </el-menu-item> </div> </el-menu> <router-view></router-view> </div> </template> <script> /* eslint-disable */ import router from "./router"; export default { name: 'App', data() { return { routes: [], } }, mounted() { this.routes = this.getRouter(); }, methods: { getRouter() { // 獲取router的配置信息 let { routes = [] } = router.options; return routes; }, handleShowItem(item) { // 點(diǎn)擊導(dǎo)航欄,切換頁(yè)面 this.$router.push({ path: item.path }) } }, } </script> <style> #app { height: auto; } </style>
其中,如果 router 里配置的 children 不存在,我們直接使用 el-menu-item 來(lái)顯示,如果 children 有值,就使用 el-submenu 來(lái)處理目錄級(jí)別,但其最終還是要用 el-menu-item 來(lái)顯示標(biāo)題內(nèi)容。另外,我們還要加上 router-view,這個(gè)標(biāo)簽才會(huì)把路由里對(duì)應(yīng)的組件渲染出來(lái)。
三、運(yùn)行與查看
我們已經(jīng)執(zhí)行過(guò)了 npm install 了,這里直接執(zhí)行 npm run serve 啟動(dòng)項(xiàng)目,并查看,如下圖(沒(méi)有做任何的樣式調(diào)整,界面有些不好看):
點(diǎn)擊 view-1-2,下方白色區(qū)域的內(nèi)容變成了 view-1-2/index.vue 的內(nèi)容。
點(diǎn)擊 view-3,下方白色區(qū)域的內(nèi)容變成了 view-3/index.vue 的內(nèi)容。
結(jié)語(yǔ)
以上內(nèi)容就可以實(shí)現(xiàn)如題所示的問(wèn)題,希望對(duì)你有所幫助
到此這篇關(guān)于vue使用路由router-view的文章就介紹到這了,更多相關(guān)vue 路由router-view內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3在自定義hooks中使用useRouter報(bào)錯(cuò)的解決方案
這篇文章主要介紹了vue3在自定義hooks中使用useRouter報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue移動(dòng)端UI框架實(shí)現(xiàn)QQ側(cè)邊菜單組件
這篇文章主要介紹了vue移動(dòng)端UI框架實(shí)現(xiàn)仿qq側(cè)邊菜單組件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03Elementui表格組件+sortablejs實(shí)現(xiàn)行拖拽排序的示例代碼
這篇文章主要介紹了Elementui表格組件+sortablejs實(shí)現(xiàn)行拖拽排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Vue2中使用axios的3種方法實(shí)例總結(jié)
axios從功能上來(lái)說(shuō)就是主要用于我們前端向后端發(fā)送請(qǐng)求,是基于http客戶端的promise,面向?yàn)g覽器和nodejs,下面這篇文章主要給大家介紹了關(guān)于Vue2中使用axios的3種方法,需要的朋友可以參考下2022-09-09vue實(shí)現(xiàn)發(fā)表評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)發(fā)表評(píng)論功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題
下面小編就為大家分享一篇解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03詳解Vue實(shí)現(xiàn)鏈接生成二維碼并支持下載
在現(xiàn)代 Web 應(yīng)用中,快速分享鏈接是一項(xiàng)常見需求,二維碼作為一種簡(jiǎn)潔的分享方式,受到了廣泛歡迎,所以本文將介紹如何使用 Vue 純前端技術(shù)實(shí)現(xiàn)動(dòng)態(tài)生成鏈接二維碼的方法,需要的可以參考下2024-03-03Vue 簡(jiǎn)單實(shí)現(xiàn)前端權(quán)限控制的示例
這篇文章主要介紹了Vue 簡(jiǎn)單實(shí)現(xiàn)前端權(quán)限控制的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Vue動(dòng)態(tài)組件加載失敗:原因、排查與解決過(guò)程
本文將探討動(dòng)態(tài)組件加載失敗的常見原因,并提供排查與解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03