Home > Web Front-end > Vue.js > Detailed explanation of how to create Vue-router sub-routes (nested routes)

Detailed explanation of how to create Vue-router sub-routes (nested routes)

藏色散人
Release: 2022-08-10 10:49:19
forward
2038 people have browsed it

In application interface development, it is usually composed of multiple layers of nested components. But as the number of pages increases, if you stuff all the pages into a routes array, it will look very messy, and you will not be able to determine which pages are related. With the help of vue-router, the nested routing function is provided, allowing us to organize related pages together. [Related recommendations: vue.js video tutorial]

Experimental purpose

In our mall project, the background management pageAdmin involves a lot Operation page, for example:

  • /admin Main page
  • /admin/create Create new information
  • /admin/edit Edit information

Let’s organize them together by nested routing.

Create Admin page

Create Admin.vue under src/views, and create an admin directory to store admin's sub-pages (when using vue-router's sub-router, you need to add it to the parent page) Component uses router-view placeholder)

  • Admin.vue

<template>
  <div class="title">
    <h1>{{ msg }}</h1>
    <!-- 路由插槽 -->
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: "home",

  data() {
    return {
      msg: "This is the Admin Page",
    };
  },
};
</script>

<style scoped>
</style>
Copy after login

Create sub-page

Create an admin directory under src/views to store admin subpages, and create Create.vue and Edit.vue under the admin directory to implement /create Create new products /editEdit product information

  • Create.vue

<template>
  <div>
    <div class="title">
      <h1>This is Admin/Create</h1>
    </div>
  </div>
</template>
Copy after login
  • Edit.vue

<template>
  <div>
    <div class="title">
      <h1>This is Admin/Edit</h1>
    </div>
  </div>
</template>
Copy after login

Modify router/index.js code

Add a sub-route. The sub-route is written by adding the children field to the original routing configuration.

children:[
    {path:&#39;/&#39;,component:xxx},
    {path:&#39;xx&#39;,component:xxx}]
Copy after login

Note: Do not add / to the path in children. Adding it means it is a route in the root directory.

  • index.js

    import Vue from &#39;vue&#39;import VueRouter from &#39;vue-router&#39;import Admin from &#39;@/views/Admin.vue&#39;// 导入admin子路由import Create from &#39;@/views/admin/Create&#39;;import Edit from &#39;@/views/admin/Edit&#39;;Vue.use(VueRouter)const routes = [
      {
        path: &#39;/admin&#39;,
        name: &#39;Admin&#39;,
        component: Admin,
        children: [
          {
            path: &#39;create&#39;,
            component: Create,
          },
          {
            path: &#39;edit&#39;,
            component: Edit,
          }
        ]
      }]const router = new VueRouter({
      routes})export default router
    Copy after login

    Now the Vue-router sub-router (nested route) is created

    In application interface development, it is usually composed of multiple layers of nested components. But as the number of pages increases, if you stuff all the pages into a routes array, it will look very messy, and you will not be able to determine which pages are related. With the help of vue-router, the nested routing function is provided, allowing us to organize related pages together.

    The above is the detailed content of Detailed explanation of how to create Vue-router sub-routes (nested routes). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template