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

Summary of horizontal scrolling website design

Horizontal scrolling isn’t appropriate in all situ...

Tomcat+Mysql high concurrency configuration optimization explanation

1.Tomcat Optimization Configuration (1) Change To...

MYSQL Left Join optimization (10 seconds to 20 milliseconds)

Table of contents 【Function Background】 [Raw SQL]...

Explanation of the configuration and use of MySQL storage engine InnoDB

MyISAM and InnoDB are the most common storage eng...

Vue.js performance optimization N tips (worth collecting)

Table of contents Functionalcomponents Childcompo...

How to understand JS function anti-shake and function throttling

Table of contents Overview 1. Function debounce 2...

What are inline elements and block elements?

1. Inline elements only occupy the width of the co...

How to use SessionStorage and LocalStorage in Javascript

Table of contents Preface Introduction to Session...

HTML+CSS merge table border sample code

When we add borders to table and td tags, double ...

Advantages and disadvantages of conditional comments in IE

IE's conditional comments are a proprietary (...

Several ways to generate unique IDs in JavaScript

Possible solutions 1. Math.random generates rando...

How to implement input checkbox to expand the click range

XML/HTML CodeCopy content to clipboard < div s...