首頁  >  文章  >  web前端  >  如何使用 Vue 實現多層導航選單?

如何使用 Vue 實現多層導航選單?

WBOY
WBOY原創
2023-06-25 09:13:303228瀏覽

隨著網路的發展,越來越多的網站需要實現多層導航選單來展示各種分類和子分類,以方便使用者的瀏覽和使用。在前端框架中,Vue 也提供了很好的支援來幫助我們實現多層導航選單。本文將介紹如何使用 Vue 實作多層導航選單。

一、基本概念

在使用Vue 實作多層導航選單之前,我們需要先了解一些基本概念:

  1. 節點(node):樹狀結構中的每一個元素都稱為一個節點。
  2. 根節點 (root node):樹狀結構中最頂層的節點稱為根節點。
  3. 葉子節點 (leaf node):樹狀結構中沒有子節點的節點稱為葉子節點。
  4. 父節點 (parent node):具有子節點的節點稱為父節點。
  5. 子節點 (child node):被父節點所包含並作為其直接後代出現的節點稱為子節點。

二、資料結構設計

在 Vue 中實作多層導航選單,我們需要定義一個資料結構來儲存選單的資料。我們可以使用 JSON 格式來儲存菜單資料。我們需要為每個選單項目定義以下屬性:

  1. id:每個選單項目都需要有一個唯一的 id。
  2. title:選單的標題。
  3. icon:選單的圖示。
  4. path:選單的連結。
  5. children:下一層選單的數據,如果目前選單是葉子節點,則 children 為空數組。

以下是一個簡單的多層選單資料範例:

[
  {
    "id": 1,
    "title": "菜单 1",
    "icon": "fa fa-home",
    "path": "/menu1",
    "children": [
      {
        "id": 11,
        "title": "菜单 1-1",
        "icon": "fa fa-book",
        "path": "/menu1-1",
        "children": [
          {
            "id": 111,
            "title": "菜单 1-1-1",
            "icon": "fa fa-link",
            "path": "/menu1-1-1",
            "children": []
          },
          {
            "id": 112,
            "title": "菜单 1-1-2",
            "icon": "fa fa-link",
            "path": "/menu1-1-2",
            "children": []
          }
        ]
      },
      {
        "id": 12,
        "title": "菜单 1-2",
        "icon": "fa fa-book",
        "path": "/menu1-2",
        "children": []
      }
    ]
  },
  {
    "id": 2,
    "title": "菜单 2",
    "icon": "fa fa-home",
    "path": "/menu2",
    "children": [
      {
        "id": 21,
        "title": "菜单 2-1",
        "icon": "fa fa-book",
        "path": "/menu2-1",
        "children": []
      },
      {
        "id": 22,
        "title": "菜单 2-2",
        "icon": "fa fa-book",
        "path": "/menu2-2",
        "children": []
      }
    ]
  }
]

三、元件設計

在Vue 中實作多層導航選單,我們可以使用元件來建構。由於多層導航選單是一棵樹狀結構,我們可以使用遞歸元件來建立樹狀結構的選單。遞歸元件是指元件在它的模板中呼叫自己。

  1. Menu 元件

Menu 元件是我們的根元件,它呼叫 MenuItem 元件來建立選單項,並根據不同的層級來設定樣式。

<template>
  <ul class="menu">
    <menu-item
      v-for="(item, index) in list"
      :key="item.id"
      :item="item"
      :level="1"
      :last="index === list.length - 1"
    ></menu-item>
  </ul>
</template>

<script>
import MenuItem from './MenuItem.vue';

export default {
  name: 'Menu',
  components: {
    MenuItem,
  },
  props: {
    list: {
      type: Array,
      required: true,
    },
  },
};
</script>

<style scoped>
.menu {
  padding: 0;
  margin: 0;
}
</style>
  1. MenuItem 元件

MenuItem 元件根據傳入的選單資料來建立選單項,並判斷目前的選單項目是否有下一層選單項,如果有則遞歸建立下一層選單,否則顯示目前選單項目的連結位址。

<template>
  <li :class="{'has-children': hasChildren}">
    <router-link :to="item.path"><i :class="item.icon"></i>{{ item.title }}</router-link>
    <ul v-if="hasChildren">
      <menu-item
        v-for="(child, index) in item.children"
        :key="child.id"
        :item="child"
        :level="level + 1"
        :last="index === item.children.length - 1"
      ></menu-item>
    </ul>
  </li>
</template>

<script>
import MenuItem from './MenuItem.vue';

export default {
  name: 'MenuItem',
  components: {
    MenuItem,
  },
  props: {
    item: {
      type: Object,
      required: true,
    },
    level: {
      type: Number,
      required: true,
    },
    last: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    hasChildren() {
      return this.item.children && this.item.children.length > 0;
    },
    indent() {
      return {
        paddingLeft: `${this.level * 20}px`,
        borderBottom: this.last ? 'none' : '',
      };
    },
  },
};
</script>

<style scoped>
.has-children {
  position: relative;
}

li i {
  margin-right: 5px;
}

ul {
  margin: 0;
  padding: 0;
}

li {
  list-style: none;
}

li:last-child {
  border-bottom: none;
}
</style>

四、使用案例

接下來我們將在一個 Vue 專案中使用我們所建立的多層導航選單元件。

  1. 建立Vue 專案

我們可以使用Vue CLI 來建立一個Vue 專案:

npm install -g @vue/cli
vue create my-project
  1. 安裝Vue 路由

我們需要使用Vue 路由來管理頁面的跳躍:

npm install vue-router --save
  1. 設定Vue 路由

我們需要在Vue 專案中設定路由,將不同的路由跳到不同的頁面。將路由的 path 設定為在選單資料中定義的 path,當使用者點選選單項目時,就會從 / 跳到對應的頁面。

import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from './views/Home.vue';
import About from './views/About.vue';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
  },
  {
    path: '/about',
    name: 'About',
    component: About,
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

export default router;
  1. 渲染多層導航選單

我們可以在頁面中使用 Menu 元件來渲染多層導航選單。

<template>
  <div id="app">
    <menu :list="menu"></menu>
    <router-view></router-view>
  </div>
</template>

<script>
import Menu from './components/Menu.vue';

export default {
  name: 'App',
  components: {
    Menu,
  },
  data() {
    return {
      menu: [
        {
          id: 1,
          title: '首页',
          icon: 'fa fa-home',
          path: '/home',
          children: [],
        },
        {
          id: 2,
          title: '关于我们',
          icon: 'fa fa-info',
          path: '/about',
          children: [],
        },
        {
          id: 3,
          title: '产品分类',
          icon: 'fa fa-product-hunt',
          path: '',
          children: [
            {
              id: 31,
              title: '手机',
              icon: 'fa fa-mobile',
              path: '/products/mobile',
              children: [
                {
                  id: 311,
                  title: '苹果',
                  icon: 'fa fa-apple',
                  path: '/products/mobile/apple',
                  children: [
                    {
                      id: 3111,
                      title: 'iPhone 12',
                      icon: 'fa fa-gift',
                      path: '/products/mobile/apple/iphone-12',
                      children: [],
                    },
                    {
                      id: 3112,
                      title: 'iPhone 11',
                      icon: 'fa fa-gift',
                      path: '/products/mobile/apple/iphone-11',
                      children: [],
                    },
                  ],
                },
                {
                  id: 312,
                  title: '华为',
                  icon: 'fa fa-huawei',
                  path: '/products/mobile/huawei',
                  children: [
                    {
                      id: 3121,
                      title: 'Mate 40 Pro',
                      icon: 'fa fa-gift',
                      path: '/products/mobile/huawei/mate-40-pro',
                      children: [],
                    },
                    {
                      id: 3122,
                      title: 'P40',
                      icon: 'fa fa-gift',
                      path: '/products/mobile/huawei/p40',
                      children: [],
                    },
                  ],
                },
              ],
            },
            {
              id: 32,
              title: '电脑',
              icon: 'fa fa-desktop',
              path: '/products/computer',
              children: [
                {
                  id: 321,
                  title: '联想',
                  icon: 'fa fa-link',
                  path: '/products/computer/lenovo',
                  children: [
                    {
                      id: 3211,
                      title: 'ThinkPad X1',
                      icon: 'fa fa-gift',
                      path: '/products/computer/lenovo/thinkpad-x1',
                      children: [],
                    },
                    {
                      id: 3212,
                      title: 'IdeaPad 5',
                      icon: 'fa fa-gift',
                      path: '/products/computer/lenovo/ideapad-5',
                      children: [],
                    },
                  ],
                },
                {
                  id: 322,
                  title: '戴尔',
                  icon: 'fa fa-dell',
                  path: '/products/computer/dell',
                  children: [
                    {
                      id: 3221,
                      title: 'XPS 13',
                      icon: 'fa fa-gift',
                      path: '/products/computer/dell/xps-13',
                      children: [],
                    },
                    {
                      id: 3222,
                      title: 'Inspiron 14 7000',
                      icon: 'fa fa-gift',
                      path: '/products/computer/dell/inspiron-14-7000',
                      children: [],
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    };
  },
};
</script>

五、總結

Vue 提供了很好的支援來幫助我們實現多層導航選單。使用遞歸組件來建立樹狀結構的選單,可以使程式碼更簡潔易懂。在設計選單資料時,需要注意屬性的命名和選單的層級關係,這有助於我們在遞歸元件中更好地實現多層導航選單。

以上是如何使用 Vue 實現多層導航選單?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn