Vue implements nested routing method example

Vue implements nested routing method example

1. Nested routing is also called sub-routing. In practical applications, it is usually composed of multiple layers of nested components. (It is actually just a nesting operation, which is quite similar to the view jump path of the backend):

2. Create a user information component and create a view component named Profile.vue in the views/user directory:

Profile.vue

<template>
  <h1>Xianyu_Turnover1</h1>
</template>
<script>
  export default {
    name: "UserList"
  }
</script>
<style scoped>
</style>

3. Create a view component named List.vue in the views/user directory in the user list component:

List.vue

<template>
  <h1>Xianyu_turnaround2</h1>
</template>
<script>
  export default {
    name: "UserList"
  }
</script>
<style scoped>
</style>

4. Modify the home page view. We modify the Main.vue view component. The ElementUI layout container component is used here. The code is as follows:

Main.vue

<template>
    <div>
      <el-container>
        <el-aside width="200px">
          <el-menu :default-openeds="['1']">
            <el-submenu index="1">
              <template slot="title"><i class="el-icon-caret-right"></i>User Management</template>
              <el-menu-item-group>
                <el-menu-item index="1-1">
                <!--Where to insert-->
                  <router-link to="/user/profile">Personal Information</router-link>
                </el-menu-item>
                <el-menu-item index="1-2">
                <!--Where to insert-->
                  <router-link to="/user/list">User list</router-link>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title"><i class="el-icon-caret-right"></i>Content Management</template>
              <el-menu-item-group>
                <el-menu-item index="2-1">Category Management</el-menu-item>
                <el-menu-item index="2-2">Content List</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-container>
          <el-header style="text-align: right; font-size: 12px">
            <el-dropdown>
              <i class="el-icon-setting" style="margin-right: 15px"></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>Personal Information</el-dropdown-item>
                <el-dropdown-item>Log out</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-header>
          <el-main>
          <!--Show the view here-->
            <router-view />
          </el-main>
        </el-container>
      </el-container>
    </div>
</template>
<script>
    export default {
        name: "Main"
    }
</script>
<style scoped lang="scss">
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
  }
  .el-aside {
    color: #333;
  }
</style>

5. Configure nested routing. Modify the index.js routing configuration file in the router directory, and use children to put it into main to write the submodule. The code is as follows:

index.js

//Import vue
import Vue from 'vue';
import VueRouter from 'vue-router';
//Import componentsimport Main from "../views/Main";
import Login from "../views/Login";
//Import submoduleimport UserList from "../views/user/List";
import UserProfile from "../views/user/Profile";

//Use Vue.use(VueRouter);
//Export export default new VueRouter({
  routes: [
    {
      //Login page path: '/main',
      component: Main,
      // Write to submodule children: [
        {
          path: '/user/profile',
          component: UserProfile,
        }, {
          path: '/user/list',
          component: UserList,
        },
      ]
    },
    //front page{
      path: '/login',
      component: Login

    },
  ]
})

6. Operation results:

7. The project structure is:

8. Then let's add a function:

Just add this code to Main.vue:

          <el-submenu index="3">
            <template slot="title"><i class="el-icon-caret-right"></i>Xianyu_turnaround management</template>
            <el-menu-item-group>
              <el-menu-item index="3-1">Xianyu_Fanshe 4</el-menu-item>
              <el-menu-item index="3-2">Xianyu_Fanshe5</el-menu-item>
            </el-menu-item-group>
          </el-submenu>

Summarize

This is the end of this article about Vue's implementation of route nesting. For more relevant Vue route nesting content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue routing vue-router usage explanation
  • Vue3 gets the current routing address
  • Detailed explanation of Vue router routing guard
  • Vue Learning - VueRouter Routing Basics
  • Specific use of routing guards in Vue
  • An article to help you understand vue routing

<<:  Example code for using @media in CSS3 to achieve web page adaptation

>>:  How to start a transaction in MySQL

Recommend

Sublime Text - Recommended method for setting browser shortcut keys

It is common to view code effects in different br...

foreman ubuntu16 quick installation

Quickstart Guide The Foreman installer is a colle...

Detailed steps for installing nodejs environment and path configuration in Linux

There are two ways to install nodejs in linux. On...

Native js to realize bouncing ball

On a whim, I wrote a case study of a small ball b...

MySQL 8.0.21 installation steps and problem solutions

Download the official website First go to the off...

Install redis and MySQL on CentOS

1|0MySQL (MariaDB) 1|11. Description MariaDB data...

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

How to reset the root password in mysql8.0.12

After installing the database, if you accidentall...

Detailed tutorial on installing mysql8.0.22 on Alibaba Cloud centos7

1. Download the MySQL installation package First ...

Detailed explanation of the role of static variables in MySQL

Detailed explanation of the role of static variab...

New ways to play with CSS fonts: implementation of colored fonts

What if you designers want to use the font below ...

How to install Maven automatically in Linux continuous integration

Unzip the Maven package tar xf apache-maven-3.5.4...

How to configure eureka in docker

eureka: 1. Build a JDK image Start the eureka con...