Home > Web Front-end > Vue.js > body text

How to use routing to achieve page jump and access in Vue

WBOY
Release: 2023-10-15 17:48:25
Original
1422 people have browsed it

How to use routing to achieve page jump and access in Vue

How to use routing to achieve page jump and access in Vue

Introduction:
In front-end development, page jump is a very common requirement. Vue provides the Vue Router plug-in to implement routing functions, which facilitates us to manage page routing in SPA (single page application). This article will introduce how to use routing to achieve page jump and access in Vue, and give specific code examples.

1. Install and configure Vue Router

  1. Install Vue Router
    Open the command line in the project directory and execute the following command to install Vue Router:

    npm install vue-router
    Copy after login
  2. Configuring Vue Router
    Create a router directory in the project, and then create an index.js file in the directory. Configure Vue Router in the index.js file. The specific code is as follows:

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    
    Vue.use(VueRouter)
    
    const routes = [
      {
     path: '/',
     name: 'Home',
     component: Home
      },
      {
     path: '/about',
     name: 'About',
     component: About
      }
    ]
    
    const router = new VueRouter({
      mode: 'history',
      base: process.env.BASE_URL,
      routes
    })
    
    export default router
    Copy after login

    Here we define two routes, one is the root path '/' and the corresponding component is Home, and the other path is '/ The corresponding component of about' is About.

2. Page jump

  1. Write a jump link
    In the Vue template, you can use tag to generate a page jump link. For example, we add a link to the About page in the App.vue component. The code is as follows:

    Copy after login
  2. Page rendering
    To display the corresponding component on the page, you need to add it to the Vue template Use the tag to render the component.

3. Page access

  1. Get routing parameters in the component
    In routing, data can be passed to the target component through parameters. For example, we obtain the routing parameters and display them in the About component. The code is as follows:

    
    
    
    Copy after login

    In the above code, we obtain the routing parameters through $route.params.id and display them on the page displayed in.

  2. Writing dynamic routing
    Sometimes the page address that needs to be jumped to is dynamically generated, and we can use dynamic routing to achieve this. For example, we create a new component Profile and define a dynamic route. The specific code is as follows:

    // 路由配置
    const routes = [
      // ...
      {
     path: '/profile/:username',
     name: 'Profile',
     component: Profile
      }
    ]
    
    // Profile组件
    
    
    
    Copy after login

    In the above code, we define a dynamic route /profile/:username, Then use $route.params.username in the Profile component to obtain the dynamic user name and display it on the page.

Summary:
This article introduces how to use routing to achieve page jump and access in Vue. By installing and configuring Vue Router, we can flexibly manage page jumps and access in SPA. I hope this article will help you learn Vue routing and let you better master Vue development skills.

The above is the detailed content of How to use routing to achieve page jump and access in Vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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 [email protected]
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!