How to configure vue sub-routing

王林
Release: 2023-05-25 11:03:08
Original
1849 people have browsed it

Vue is a popular JavaScript framework for building interactive web applications. Vue's routing function allows us to create a single page application (SPA). Different routes correspond to different components, and then the specified content is rendered in the component. Subroutes are a special type of routing in Vue routing that can be nested within other routes. This article will introduce how to configure subroutes in Vue.

First, we need to ensure that Vue and its related dependencies have been installed. It is best to create a new project using the Vue CLI. Next, we need to create the Vue component and define the routes.

Define the parent route
Define the route in the parent component (usually the root route/) to provide a container for the child component.

// src/router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
Copy after login

Define sub-route
Then, create a route pointing to the sub-component in the parent component.

// src/router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: 'about',
        name: 'About',
        component: () => import('../views/About.vue')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
Copy after login

In this example, "/" is the root route, which points to the Home component. We created a sub-route for the Home component as "/about", which points to the About component.

Add a child component container in the parent component template
In the template of the Home component, we need to add a placeholder container to render the child component.

<template>
  <div>
    <h1>Home</h1>
    <router-view></router-view>
  </div>
</template>
Copy after login

This "router-view" directive is the subcomponent container, which will display the components specified through the subroute.

Add content in the child component template
In the template of the About component, we can add some content to show what it is.

<template>
  <div>
    <h1>About</h1>
    <p>This is an about page.</p>
  </div>
</template>
Copy after login

Now, we have successfully configured a Vue sub-router. Test it to see if it displays properly.

Summary
There are three basic steps to configure Vue subrouting. These steps include:

Define the parent route: Declare the parent component in the root route, and navigate its route to this component.

Define sub-route: Create a routing pointer for the sub-component in the parent component.

Add a child component container in the parent component template: insert the child component into the placeholder element.

Add content in the subcomponent template: Provide content for the subcomponent to demonstrate its functionality.

Finally, as a reminder, when you use subroutes in Vue, you must keep the path correct. The path must start from the parent component, starting with a slash (/).

The above is the detailed content of How to configure vue sub-routing. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!