Do you know what are the ways to jump routes in Vue?

Do you know what are the ways to jump routes in Vue?

The first method: router-link (declarative routing)

1. Without parameters <router-link :to="{name:'home'}"> 
<router-link :to="{path:'/home'}"> //either name or path is fine, name is recommended  
// Note: If the link in router-link starts with '/', it starts from the root route. If it does not start with '/', it starts from the current route.
2. With parameters <router-link :to="{name:'home', params: {id:1}}">  
// params pass parameters (similar to post)
// Routing configuration path: "/home/:id" or path: "/home:id" 
// If you don't configure path, you can request it for the first time, but the id will disappear when you refresh the page // If you configure path, the id will be retained when you refresh the page // HTML takes the parameter $route.params.id
// script takes the parameter this.$route.params.id
<router-link :to="{name:'home', query: {id:1}}"> 

The second method: router.push (programmatic routing)

// string router.push('home')
// Object router.push({ path: 'home' })
// Named routes router.push({ name: 'user', params: { userId: '123' }})
// With query parameters, becomes /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})

Note: If path is provided, params will be ignored, which is not the case for query in the above example. Instead, you need to provide a route name or write the full path with parameters as shown in the following example:

const userId = '123'
router.push({ name: 'user', params: { userId }}) // -> /user/123
router.push({ path: `/user/${userId}` }) // -> /user/123
// The params here are not effective router.push({ path: '/user', params: { userId }}) // -> /user

The third way: this.$router.push() (call in the function)

1. without parameters this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2. query parameter this.$router.push({name:'home',query: {id:'1'}})
this.$router.push({path:'/home',query: {id:'1'}})
// html takes the parameter $route.query.id
// script takes the parameter this.$route.query.id
3. params parameter this.$router.push({name:'home',params: {id:'1'}}) // Only name can be used
// Routing configuration path: "/home/:id" or path: "/home:id" ,
// If you don't configure path, you can request it for the first time, but the id will disappear when you refresh the page // If you configure path, the id will be retained when you refresh the page // HTML takes the parameter $route.params.id
// script takes the parameter this.$route.params.id
4. The difference between query and params. Query is similar to get. After the page is redirected, the parameters will be added to the URL, similar to ?id=1. Non-important parameters can be passed in this way. Passwords and the like can still be passed in params. The refresh page id is still in params. Similar to post, after the page is redirected, the parameters will not be added to the URL, but the refresh page id will disappear. **Note: To get the parameters on the route, use $route, without r at the end**

The fourth method: this.$router.replace() (same usage as above, push)

The fifth way: this.$router.go(n)

this.$router.go(n)
Jump forward or backward n pages, n can be a positive or negative integer ps: difference from this.$router.push
Jump to the specified URL path and add a record to the history stack. Clicking back will return to the previous page this.$router.replace
Jump to the specified URL path, but there will be no record in the history stack. Clicking back will jump to the previous page (that is, directly replace the current page)
this.$router.go(n)
Jump forward or backward n pages, n can be a positive or negative integer

Params is part of the route and is required. Query is the parameter concatenated after the url. It doesn't matter if there is no query.
Once params is set in a route, it becomes part of the route. If the route has params passed in, but the parameter is not passed during redirection, the redirection will fail or the page will have no content.

You can pass parameters even if params and query are not set. However, if params is not set, the parameters will be lost when the page is refreshed or returned.

Both can pass parameters, what is the difference?

The query parameter is configured with path, while the params parameter is configured with name. Configuring path in params is invalid.

query does not need to set parameters in the routing configuration, but params must be set

The parameters passed by the query will be displayed in the address bar

The refresh of params will be invalid, but query will save the passed value and remain unchanged after refresh

refer to:

https://www.jb51.net/article/183611.htm

vue.js official website

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Implementation of nested jump of vue routing view router-view
  • Vue routing relative path jump method
  • Solution for Vue routing this.route.push jump page not refreshing
  • Routing in Vue is not counted in history operations
  • Vue same route jump forced refresh the route component operation

<<:  Cross-browser local storage Ⅰ

>>:  Detailed explanation of the difference between in and exists in MySQL

Recommend

CSS3 creates 3D cube loading effects

Brief Description This is a CSS3 cool 3D cube pre...

Example of how to import nginx logs into elasticsearch

The nginx logs are collected by filebeat and pass...

Manual and scheduled backup steps for MySQL database

Table of contents Manual backup Timer backup Manu...

How to solve the problem of case insensitivity in MySQL queries

question Recently, when I was completing a practi...

Introduction to vim plugin installation under Linux system

Table of contents Install vim plugin manager Add ...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

How to implement mysql database backup in golang

background Navicat is the best MySQL visualizatio...

How to install vncserver in Ubuntu 20.04

Ubuntu 20.04 has been officially released in Apri...

Detailed explanation of Linux DMA interface knowledge points

1. Two types of DMA mapping 1.1. Consistent DMA m...

How to handle MySQL numeric type overflow

Now, let me ask you a question. What happens when...

How to set up jar application startup on CentOS7

Pitfalls encountered during project deployment Wh...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...