vue.js Router nested routes

vue.js Router nested routes

Preface:

Sometimes in a route, the main part is the same, but the underlying structure may be different. For example, when you visit the home page, there is /home/news for news and /home/message for information. This is when nested routing comes into play.

The project structure is as follows:

We created three components, namely Home.vue , HomeNews.vue , HomeMessage.vue , the code is as follows:

Home.vue

<template>
  <div class="home">
    <h1>Home</h1>
    <router-link to="/home/news">News</router-link> // Note that you must write the full path here. You cannot just write /news. You need to add /home
    <router-link to="/home/message">Information class</router-link>
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: "Home",
};
</script>

<style scoped>

</style>

HomeNews

<template>
  <div class="homeNews">
    <ul>
      <li>News 1</li>
      <li>News 2</li>
      <li>News 3</li>
      <li>News 4</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "HomeNews"
}
</script>

<style scoped>

</style>

HomeMessage

<template>
  <div class="homeMessage">
    <ul>
      <li>Message 1</li>
      <li>Message 2</li>
      <li>Message 3</li>
      <li>Message 4</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "HomeMessage"
}
</script>

<style scoped>

</style>

After the component is written, we configure the route in the index.js file in the router folder

import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

// Here we still use routing lazy loading const Home = () => import('../views/Home')
const HomeNews = () => import('../views/HomeNews')
const HomeMessage = () => import('../views/HomeMessage')

const routes = [
  {
    path: "/home",
    name: "Home",
    component: Home,
    // How to write child routes children: [
      {
        path: "news",
        name: "HomeNews",
        component: HomeNews
      },
      {
        path: "message",
        name: "HomeMessage",
        component: HomeMessage
      },
    ]
  },
  {
    path: "",
    redirect: "home"
  }
];

const router = new VueRouter({
  routes,
  mode: 'history',
});

export default router;

Nested routes are very simple to write. You will find that the children configuration is an array of route configurations just like routes configuration, so you can nest multiple layers of routes.

At this point, based on the above configuration, when you visit /home/ , home exit will not render anything.

This is because no suitable sub-route was matched. If you want to render something, you can provide an empty child route:

const routes = [
  {
    path: "/home",
    name: "Home",
    component: Home,
    children: [
      {
        path: "news",
        name: "HomeNews",
        component: HomeNews
      },
      {
        path: "message",
        name: "HomeMessage",
        component: HomeMessage
      },
      // Add an empty sub-route {
        path: "",
        redirect: "news"
      }
    ]
  },

  {
    path: "",
    redirect: "home"
  }
];

In this way, the page will be redirected to the news page by default, and news information will be displayed.

This is the end of this article about vue.js Router nested routing. For more related vue nested routing 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:
  • Detailed explanation of Vue-router nested routing
  • Detailed explanation of Vue routing router
  • Detailed explanation of the two modes of Router routing in Vue: hash and history
  • Implementation of nested jump of vue routing view router-view
  • Detailed explanation of VueRouter routing
  • Detailed explanation of Vue router routing

<<:  CSS removes the background color of elements generated when they are clicked on the mobile terminal (recommended)

>>:  The best solution for resetting the root password of MySQL 8.0.23

Recommend

mysql8 Common Table Expression CTE usage example analysis

This article uses an example to describe how to u...

Summary of Mysql high performance optimization skills

Database Command Specification All database objec...

How to install MySQL 5.7.17 and set the encoding to utf8 in Windows

download MySQL official download, select Windows ...

Detailed explanation of JavaScript stack and copy

Table of contents 1. Definition of stack 2. JS st...

What to do if you forget your Linux/Mac MySQL password

What to do if you forget your Linux/Mac MySQL pas...

Personalized and creative website design examples (30)

Therefore, we made a selection of 30 combinations ...

Solve the compatibility issue between MySQL 8.0 driver and Alibaba Druid version

This article mainly introduces the solution to th...

How to prevent event bubbling in JavaScript

What we need to pay attention to is that the char...

jenkins+gitlab+nginx deployment of front-end application

Table of contents Related dependency installation...

Analysis of the Poor Performance Caused by Large Offset of LIMIT in MySQL Query

Preface We all know that MySQL query uses the sel...

How to connect JDBC to MySQL 5.7

1. First prepare the MySQL and Eclipse environmen...