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

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

Usage of HTML H title tag

The usage of H tags, especially h1, has always bee...

Using react-beautiful-dnd to implement drag and drop between lists

Table of contents Why choose react-beautiful-dnd ...

Radio buttons and multiple-choice buttons are styled using images

I've seen people asking before, how to add sty...

Detailed steps to delete environment variables in Linux

How to delete environment variables in Linux? Use...

MySQL statement arrangement and summary introduction

SQL (Structured Query Language) statement, that i...

Display flex arrangement in CSS (layout tool)

Regarding display: flex layout, some people have ...

Detailed explanation of simple snow effect example using JS

Table of contents Preface Main implementation cod...

MySQL InnoDB MRR Optimization Guide

Preface MRR is the abbreviation of Multi-Range Re...

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

How to use bar charts in Vue and modify the configuration yourself

1. Import echart in HTML file <!-- Import echa...