Home > Web Front-end > Front-end Q&A > How to display child routes in parent routes in vue

How to display child routes in parent routes in vue

PHPz
Release: 2023-04-17 14:32:49
Original
1151 people have browsed it

Vue.js is a modern JavaScript framework for building single-page web applications. It provides some useful features such as routing. Using Vue Router we can easily manage navigation and state in our application.

In Vue router, routes are organized into a tree structure. We create a parent route and some child routes and then group them together. This way we can organize different page parts together and display them on different pages in the main application.

Generally speaking, child routes will not be displayed in parent routes. Instead, we combine them together and ultimately render them onto the main application's root route. This process can be achieved through the "named view" feature of Vue routing.

Named views are a Vue router feature that allows us to render multiple views in a single routed component. In this case, each view has a unique name and can be used in the parent component.

Using named views, we can render child routing components at specific positions within the parent routing component. This location is identified by a named view tag with the same name.

For example, suppose we have a parent route that contains three child routes: Home, About, Contact. We can combine them together using the following code:

<template>
  <div>
    <header>
      <router-link to="/">Home</router-link>
      <router-link to="/about">About</router-link>
      <router-link to="/contact">Contact</router-link>
    </header>

    <main>
      <router-view name="home"></router-view>
      <router-view name="about"></router-view>
      <router-view name="contact"></router-view>
    </main>
  </div>
</template>

<script>
export default {
  name: 'ParentComponent',
  components: {}
}
</script>
Copy after login

In the above code, we have used three named view tags and named them "home", "about" and "contact". This name can be passed to the child component using props and used within the child component.

Then, within our child components, we can use the route configuration object to specify which component should be rendered in which named view. For example:

const router = new VueRouter({
  routes: [
    {
      path: '/',
      components: {
        default: HomeComponent,
        home: SidebarComponent
      }
    },
    {
      path: '/about',
      components: {
        default: AboutComponent,
        about: SidebarComponent
      }
    },
    {
      path: '/contact',
      components: {
        default: ContactComponent,
        contact: SidebarComponent
      }
    }
  ]
})
Copy after login

In the above code, we specified a named view for each child route. For example, in the "home" route, we used the "sidebar" components and placed them in the "home" named view. Likewise, we use them in the "about" and "contact" routes and put them in the response's named view.

In short, using Vue.js’ router, we can easily manage navigation and state in the application. Through the use of named views, we can render child routing components at a specific location of the parent routing component. This allows us to create complex single-page applications and manage them in a clear and organized way.

The above is the detailed content of How to display child routes in parent routes in vue. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template