Home  >  Article  >  Web Front-end  >  Examples to explain how vue implements parent routing to jump to sub-routes

Examples to explain how vue implements parent routing to jump to sub-routes

PHPz
PHPzOriginal
2023-04-07 09:32:572242browse

In Vue, we often need to perform routing jump operations. Vue's routing function is very powerful and supports functions such as nested routing and dynamic routing. This article will describe how to jump from a parent route to a child route.

  1. Sub route configuration

First, we need to configure the sub route in the parent route. In Vue, you can use the Vue Router plug-in to implement routing management and control. The following is how the child route is configured:

const routes = [
    {
        path: '/parent',
        component: ParentComponent,
        children: [
            {
                path: 'child',
                component: ChildComponent
            }
        ]
    }
]

In the above code, we first define a parent route /parent and specify its component as ParentComponent. Then a child route /parent/child is defined in the children array, and its component is specified as ChildComponent.

  1. Route jump

In the parent route, we can use the <router-link> tag to jump to the child route. <router-link>The tag will automatically handle route jumps and generate corresponding links. The following is a simple example:

<template>
    <div>
        <h1>Parent Component</h1>
        <router-link to="/parent/child">Go to Child Component</router-link>
        <router-view></router-view>
    </div>
</template>

In the above code, we first use the <router-link> tag to generate a link, and the link path is /parent/ child, this is the child routing path we defined. When the user clicks this link, it will automatically jump to the sub-route /parent/child. At the same time, in the parent route, we use the <router-view> tag to render the child route component.

  1. Sub-routing with parameters

In actual development, we often need to pass parameters in routing. Vue Router supports passing parameters in routes, and also supports accessing passed parameters through route navigation objects. The following is an example of a sub-route with parameters:

const routes = [
    {
        path: '/parent',
        component: ParentComponent,
        children: [
            {
                path: 'child/:id',
                component: ChildComponent
            }
        ]
    }
]

In the above code, we define a sub-route with parameters /parent/child/:id, where :id represents the parameter name. In the child route component, we can access the passed parameters through $route.params. Here is a simple example:

<template>
    <div>
        <h1>Child Component</h1>
        <p>Id: {{ $route.params.id }}</p>
    </div>
</template>

In the above code, we access the passed parameters through $route.params.id. When the user accesses /parent/child/123, the value of the parameter id is 123.

  1. Summary

This article briefly introduces how to implement parent routing to jump sub-routing in Vue. By defining sub-routes and using the <router-link> tag to jump to routes, we can easily implement routing navigation functions in Vue applications. At the same time, Vue Router also supports passing parameters in routing, which is very convenient and practical.

The above is the detailed content of Examples to explain how vue implements parent routing to jump to sub-routes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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