最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用路由router-view的詳細(xì)代碼

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

前言: router-viewNavMenu 導(dǎo)航欄的配合,在 web 應(yīng)用中極為常見。其原理就是采用 SPA(single-page-application) 模式,就是只有一個 Web 頁面的應(yīng)用,通過 router 來控制頁面的刷新和迭代。

提示: 以下示例基于 vue2.6.14 版本,vue-router3.5.2 版本,element-ui2.15.12 版本。
好了,廢話不多說,我們直接貼上全部代碼

一、創(chuàng)建vue頁面與js腳本

1、 如下圖,我們在項(xiàng)目的 src 目錄里,新建 router 文件夾,并創(chuàng)建 index.js 文件。新建 views 目錄,并把我們的 vue 頁面全部放在這里。

2、 其中每個 index.vue 的內(nèi)容都很簡單,只有一行文字

<template>
  <div>view-1</div>
</template>
<script>
/* eslint-disable */
export default {
  name: 'view-1',
  mounted() {
  },
}
</script>

3、 另外 package.json 的依賴關(guān)系也一并貼上, 至此可以執(zhí)行 npm install 來安裝依賴庫

二、編寫腳本

1、編輯router目錄的index.js文件 (這里只做了二級目錄的深度)

import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
/*
* 我們在項(xiàng)目中使用router.push或router.replace時,如果在A界面繼續(xù)跳轉(zhuǎn)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的內(nèi)容渲染到頁面上
      meta: { title: 'view-1', keepAlive: true, icon: "el-icon-document-copy" },
      children: [
        {
          path: "/view-1/view-1-1", // 這個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)航欄,切換頁面
      this.$router.push({
        path: item.path
      })
    }
  },
}
</script>
<style>
#app {
  height: auto;
}
</style>

其中,如果 router 里配置的 children 不存在,我們直接使用 el-menu-item 來顯示,如果 children 有值,就使用 el-submenu 來處理目錄級別,但其最終還是要用 el-menu-item 來顯示標(biāo)題內(nèi)容。另外,我們還要加上 router-view,這個標(biāo)簽才會把路由里對應(yīng)的組件渲染出來。

三、運(yùn)行與查看

我們已經(jīng)執(zhí)行過了 npm install 了,這里直接執(zhí)行 npm run serve 啟動項(xiàng)目,并查看,如下圖(沒有做任何的樣式調(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é)語

以上內(nèi)容就可以實(shí)現(xiàn)如題所示的問題,希望對你有所幫助

到此這篇關(guān)于vue使用路由router-view的文章就介紹到這了,更多相關(guān)vue 路由router-view內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue自定義指令directive實(shí)例詳解

    vue自定義指令directive實(shí)例詳解

    這篇文章主要介紹了vue自定義指令directive的相關(guān)資料,下面通過實(shí)例給大家介紹,需要的朋友可以參考下
    2018-01-01
  • Vue 去除路徑中的#號

    Vue 去除路徑中的#號

    大家都知道vue-router有兩種模式,hash模式和history模式,帶#的則是hash模式。接下來給大家?guī)砹薞ue 去除路徑中的#號的解決方法,感興趣的朋友一起看看吧
    2018-04-04
  • vue 如何使用遞歸組件

    vue 如何使用遞歸組件

    這篇文章主要介紹了vue 如何使用遞歸組件,幫助大家更好的理解和使用vue,完成開發(fā)需求,感興趣的朋友可以了解下
    2020-10-10
  • 關(guān)于this.$refs獲取不到dom的可能原因及解決方法

    關(guān)于this.$refs獲取不到dom的可能原因及解決方法

    這篇文章主要介紹了關(guān)于this.$refs獲取不到dom的可能原因及解決方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用

    Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用

    這篇文章主要介紹了Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue設(shè)置頁面超時15分鐘自動退出登錄的方法詳解

    vue設(shè)置頁面超時15分鐘自動退出登錄的方法詳解

    當(dāng)用戶登錄后,如果長時間未操作頁面這個時候需要自動退出登錄回到登錄頁面,本文將給大家介紹一下vue設(shè)置頁面超時15分鐘自動退出登錄的方法,感興趣的同學(xué)可以自己動手試一下
    2023-10-10
  • VUE PC端可拖動懸浮按鈕的實(shí)現(xiàn)代碼

    VUE PC端可拖動懸浮按鈕的實(shí)現(xiàn)代碼

    這篇文章主要介紹了VUE PC端可拖動懸浮按鈕的實(shí)現(xiàn)代碼,通過實(shí)例代碼介紹了父頁面引用的方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • Vue的虛擬DOM和diff算法你了解嗎

    Vue的虛擬DOM和diff算法你了解嗎

    這篇文章主要為大家詳細(xì)介紹了Vue的虛擬DOM和diff算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

    Vue插槽_特殊特性slot,slot-scope與指令v-slot說明

    這篇文章主要介紹了Vue插槽_特殊特性slot,slot-scope與指令v-slot說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue中狀態(tài)管理器Pinia的用法詳解

    vue中狀態(tài)管理器Pinia的用法詳解

    Pinia?是?Vue.js?的輕量級狀態(tài)管理庫,最近很受歡迎,它使用?Vue?3?中的新反應(yīng)系統(tǒng)來構(gòu)建一個直觀且完全類型化的狀態(tài)管理庫,下面就跟隨小編一起來學(xué)習(xí)一下它的具體使用吧
    2023-10-10

最新評論

吴堡县| 兰坪| 南和县| 定日县| 海南省| 乡宁县| 浦江县| 晋江市| 云和县| 新乡县| 师宗县| 台南市| 信阳市| 海安县| 兴海县| 商水县| 固原市| 松原市| 海盐县| 茶陵县| 密云县| 于都县| 依安县| 定南县| 上高县| 五河县| 柳江县| 内丘县| 曲麻莱县| 阜阳市| 大冶市| 库车县| 佛山市| 隆德县| 长宁县| 金华市| 黄大仙区| 五指山市| 平昌县| 东阳市| 昆明市|