亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue使用路由router-view的詳細代碼

 更新時間:2023年12月06日 14:39:33   作者:知之、行之、思之  
這篇文章主要介紹了vue使用路由router-view的相關知識,其原理就是采用?SPA(single-page-application)?模式,就是只有一個?Web?頁面的應用,通過?router?來控制頁面的刷新和迭代,感興趣的朋友一起看看吧

前言: router-viewNavMenu 導航欄的配合,在 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報錯的解決方案

    這篇文章主要介紹了vue3在自定義hooks中使用useRouter報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue移動端UI框架實現QQ側邊菜單組件

    vue移動端UI框架實現QQ側邊菜單組件

    這篇文章主要介紹了vue移動端UI框架實現仿qq側邊菜單組件,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-03-03
  • Elementui表格組件+sortablejs實現行拖拽排序的示例代碼

    Elementui表格組件+sortablejs實現行拖拽排序的示例代碼

    這篇文章主要介紹了Elementui表格組件+sortablejs實現行拖拽排序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Vue2中使用axios的3種方法實例總結

    Vue2中使用axios的3種方法實例總結

    axios從功能上來說就是主要用于我們前端向后端發(fā)送請求,是基于http客戶端的promise,面向瀏覽器和nodejs,下面這篇文章主要給大家介紹了關于Vue2中使用axios的3種方法,需要的朋友可以參考下
    2022-09-09
  • vue實現發(fā)表評論功能

    vue實現發(fā)表評論功能

    這篇文章主要為大家詳細介紹了vue實現發(fā)表評論功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 解決vue頁面刷新或者后退參數丟失的問題

    解決vue頁面刷新或者后退參數丟失的問題

    下面小編就為大家分享一篇解決vue頁面刷新或者后退參數丟失的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 詳解Vue實現鏈接生成二維碼并支持下載

    詳解Vue實現鏈接生成二維碼并支持下載

    在現代 Web 應用中,快速分享鏈接是一項常見需求,二維碼作為一種簡潔的分享方式,受到了廣泛歡迎,所以本文將介紹如何使用 Vue 純前端技術實現動態(tài)生成鏈接二維碼的方法,需要的可以參考下
    2024-03-03
  • Vue 簡單實現前端權限控制的示例

    Vue 簡單實現前端權限控制的示例

    這篇文章主要介紹了Vue 簡單實現前端權限控制的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Vue動態(tài)組件加載失敗:原因、排查與解決過程

    Vue動態(tài)組件加載失敗:原因、排查與解決過程

    本文將探討動態(tài)組件加載失敗的常見原因,并提供排查與解決方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Vue組件之間四種通信方式詳解

    Vue組件之間四種通信方式詳解

    vue框架提供了前端開發(fā)組件的思想,可以通過組件來組合成一個完整的頁面,都是隨著組件數量原來越多,組件之間難免需要相互通信。本文將為大家介紹四種組件間的通信方式,需要的可以參考一下
    2022-01-01

最新評論