Table of Contents
Hello Vue Router!
About Page
Profile Page
Home Web Front-end Vue.js How to use routing to achieve page jump and access in Vue

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

Oct 15, 2023 pm 05:48 PM
routing access Page jump

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<router- link> 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:

    <template>
      <div>
     <h1 id="Hello-Vue-Router">Hello Vue Router!</h1>
     <router-link to="/about">About</router-link>
     <router-view></router-view>
      </div>
    </template>
    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 <router-view> 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:

    <template>
      <div>
     <h2 id="About-Page">About Page</h2>
     <p>参数:{{ $route.params.id }}</p>
      </div>
    </template>
    
    <script>
    export default {
      name: 'About'
    }
    </script>
    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组件
    <template>
      <div>
     <h2 id="Profile-Page">Profile Page</h2>
     <p>用户名:{{ $route.params.username }}</p>
      </div>
    </template>
    
    <script>
    export default {
      name: 'Profile'
    }
    </script>
    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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions Nov 18, 2023 pm 05:08 PM

Detailed explanation of PHP page jump functions: Page jump techniques for header, location, redirect and other functions, which require specific code examples. Introduction: When developing a Web website or application, jumping between pages is an essential function. PHP provides a variety of ways to implement page jumps, including header functions, location functions, and jump functions provided by some third-party libraries, such as redirect. This article will introduce in detail how to use these functions

iOS 17: How to control which apps can access your photos iOS 17: How to control which apps can access your photos Sep 13, 2023 pm 09:09 PM

In iOS17, Apple has more control over what apps can see in photos. Read on to learn how to manage app access by app. In iOS, Apple's in-app photo picker lets you share specific photos with the app, while the rest of your photo library remains private. Apps must request access to your entire photo library, and you can choose to grant the following access to apps: Restricted Access – Apps can only see images that you can select, which you can do at any time in the app or by going to Settings &gt ;Privacy & Security&gt;Photos to view selected images. Full access – App can view photos

How to access JSONNode's JSON fields, arrays and nested objects in Java? How to access JSONNode's JSON fields, arrays and nested objects in Java? Aug 30, 2023 pm 11:05 PM

A JsonNode is Jackson's JSON tree model that can read JSON into JsonNode instances and write JsonNode into JSON. We can use Jackson to read JSON into a JsonNode by creating an ObjectMapper instance and calling the readValue() method. We can access fields, arrays or nested objects using the get() method of the JsonNode class. We can use the asText() method to return a valid string representation and convert the node's value to Javaint using the asInt() method of the JsonNode class. In the example below we can access Json

Access metadata of various audio and video files using Python Access metadata of various audio and video files using Python Sep 05, 2023 am 11:41 AM

We can access the metadata of audio files using Mutagen and the eyeD3 module in Python. For video metadata we can use movies and the OpenCV library in Python. Metadata is data that provides information about other data, such as audio and video data. Metadata for audio and video files includes file format, file resolution, file size, duration, bitrate, etc. By accessing this metadata, we can manage media more efficiently and analyze the metadata to obtain some useful information. In this article, we will take a look at some of the libraries or modules provided by Python for accessing metadata of audio and video files. Access audio metadata Some libraries for accessing audio file metadata are - using mutagenesis

How to solve the problem of inaccessibility after Tomcat deploys war package How to solve the problem of inaccessibility after Tomcat deploys war package Jan 13, 2024 pm 12:07 PM

How to solve the problem that Tomcat cannot successfully access the war package after deploying it requires specific code examples. As a widely used Java Web server, Tomcat allows developers to package their own developed Web applications into war files for deployment. However, sometimes we may encounter the problem of being unable to successfully access the war package after deploying it. This may be caused by incorrect configuration or other reasons. In this article, we'll provide some concrete code examples that address this dilemma. 1. Check Tomcat service

Java Apache Camel: Building a flexible and efficient service-oriented architecture Java Apache Camel: Building a flexible and efficient service-oriented architecture Feb 19, 2024 pm 04:12 PM

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

Use uniapp to achieve page jump animation effect Use uniapp to achieve page jump animation effect Nov 21, 2023 pm 02:15 PM

Title: Using uniapp to achieve page jump animation effect In recent years, the user interface design of mobile applications has become one of the important factors in attracting users. Page jump animation effects play an important role in improving user experience and visualization effects. This article will introduce how to use uniapp to achieve page jump animation effects, and provide specific code examples. uniapp is a cross-platform application development framework developed based on Vue.js. It can compile and generate applications for multiple platforms such as mini programs, H5, and App through a set of codes.

How to solve external resource access and calls in PHP development How to solve external resource access and calls in PHP development Oct 08, 2023 am 11:01 AM

How to solve the problem of accessing and calling external resources in PHP development requires specific code examples. In PHP development, we often encounter situations where we need to access and call external resources, such as API interfaces, third-party libraries or other server resources. When dealing with these external resources, we need to consider how to access and call safely while ensuring performance and reliability. This article describes several common solutions and provides corresponding code examples. 1. Use the curl library to call external resources. Curl is a very powerful open source library.

See all articles