vue使用路由router-view的詳細代碼
前言: router-view 與 NavMenu 導航欄的配合,在 web 應用中極為常見。其原理就是采用 SPA(single-page-application) 模式,就是只有一個 Web 頁面的應用,通過 router 來控制頁面的刷新和迭代。
提示: 以下示例基于 vue2.6.14 版本,vue-router3.5.2 版本,element-ui2.15.12 版本。
好了,廢話不多說,我們直接貼上全部代碼
一、創(chuàng)建vue頁面與js腳本
1、 如下圖,我們在項目的 src 目錄里,新建 router 文件夾,并創(chuàng)建 index.js 文件。新建 views 目錄,并把我們的 vue 頁面全部放在這里。
2、 其中每個 index.vue 的內容都很簡單,只有一行文字
<template> <div>view-1</div> </template> <script> /* eslint-disable */ export default { name: 'view-1', mounted() { }, } </script>
3、 另外 package.json 的依賴關系也一并貼上, 至此可以執(zhí)行 npm install 來安裝依賴庫
二、編寫腳本
1、編輯router目錄的index.js文件 (這里只做了二級目錄的深度)
import Vue from "vue"; import VueRouter from "vue-router"; Vue.use(VueRouter); /* * 我們在項目中使用router.push或router.replace時,如果在A界面繼續(xù)跳轉A界面,就會拋出異常報錯。 * 這里處理一下捕獲異常的邏輯,就可以避免報錯了。 */ 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的內容渲染到頁面上 meta: { title: 'view-1', keepAlive: true, icon: "el-icon-document-copy" }, children: [ { path: "/view-1/view-1-1", // 這個path可以去掉 /view-1,這里只是方便知道其從屬關系 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、編輯項目的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、編輯項目的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) { // 點擊導航欄,切換頁面 this.$router.push({ path: item.path }) } }, } </script> <style> #app { height: auto; } </style>
其中,如果 router 里配置的 children 不存在,我們直接使用 el-menu-item 來顯示,如果 children 有值,就使用 el-submenu 來處理目錄級別,但其最終還是要用 el-menu-item 來顯示標題內容。另外,我們還要加上 router-view,這個標簽才會把路由里對應的組件渲染出來。
三、運行與查看
我們已經執(zhí)行過了 npm install 了,這里直接執(zhí)行 npm run serve 啟動項目,并查看,如下圖(沒有做任何的樣式調整,界面有些不好看):
點擊 view-1-2,下方白色區(qū)域的內容變成了 view-1-2/index.vue 的內容。
點擊 view-3,下方白色區(qū)域的內容變成了 view-3/index.vue 的內容。
結語
以上內容就可以實現如題所示的問題,希望對你有所幫助
到此這篇關于vue使用路由router-view的文章就介紹到這了,更多相關vue 路由router-view內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue3在自定義hooks中使用useRouter報錯的解決方案
這篇文章主要介紹了vue3在自定義hooks中使用useRouter報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Elementui表格組件+sortablejs實現行拖拽排序的示例代碼
這篇文章主要介紹了Elementui表格組件+sortablejs實現行拖拽排序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08