Home> Web Front-end> Vue.js> body text

How to use Vue to implement a multi-level navigation menu?

WBOY
Release: 2023-06-25 09:13:30
Original
3188 people have browsed it

With the development of the Internet, more and more websites need to implement multi-level navigation menus to display various categories and sub-categories to facilitate users' browsing and use. In the front-end framework, Vue also provides good support to help us implement multi-level navigation menus. This article will introduce how to use Vue to implement a multi-level navigation menu.

1. Basic concepts

Before using Vue to implement multi-level navigation menus, we need to understand some basic concepts:

  1. Node (node): tree structure Each element in is called a node.
  2. Root node (root node): The top-most node in the tree structure is called the root node.
  3. Leaf node: A node that has no child nodes in the tree structure is called a leaf node.
  4. Parent node: A node with child nodes is called a parent node.
  5. Child node: A node that is contained by a parent node and appears as its direct descendant is called a child node.

2. Data structure design

To implement a multi-level navigation menu in Vue, we need to define a data structure to store the menu data. We can use JSON format to store menu data. We need to define the following properties for each menu item:

  1. id: Each menu item needs to have a unique id.
  2. title: The title of the menu.
  3. icon: The icon of the menu.
  4. path: Menu link.
  5. children: data of the next-level menu. If the current menu is a leaf node, children is an empty array.

The following is a simple multi-level menu data example:

[ { "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": [] } ] } ]
Copy after login

3. Component design

To implement multi-level navigation menus in Vue, we can use components to build. Since the multi-level navigation menu is a tree structure, we can use recursive components to create a tree-structured menu. A recursive component is when a component calls itself within its template.

  1. Menu component

The Menu component is our root component, which calls the MenuItem component to create menu items and set styles according to different levels.

  
Copy after login
  1. MenuItem component

The MenuItem component creates menu items based on the incoming menu data and determines whether the current menu item has a next-level menu item. If so, Then the next-level menu is created recursively, otherwise the link address of the current menu item is displayed.

  
Copy after login

4. Use cases

Next we will use the multi-level navigation menu component we created in a Vue project.

  1. Create Vue project

We can use Vue CLI to create a Vue project:

npm install -g @vue/cli vue create my-project
Copy after login
  1. Install Vue routing

We need to use Vue routing to manage page jumps:

npm install vue-router --save
Copy after login
  1. Configuring Vue routing

We need to configure routing in the Vue project to separate different Routing jumps to different pages. Set the path of the route to the path defined in the menu data. When the user clicks on the menu item, they will jump from / to the corresponding page.

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;
Copy after login
  1. Rendering multi-level navigation menu

We can use the Menu component in the page to render the multi-level navigation menu.

 
Copy after login

5. Summary

Vue provides good support to help us implement multi-level navigation menus. Using recursive components to create tree-structured menus can make the code simpler and easier to understand. When designing menu data, you need to pay attention to the naming of attributes and the hierarchical relationship of the menu, which helps us better implement multi-level navigation menus in recursive components.

The above is the detailed content of How to use Vue to implement a multi-level navigation menu?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn