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. 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 SummarizeThis 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:
|
<<: Cross-browser local storage Ⅰ
>>: Detailed explanation of the difference between in and exists in MySQL
This article shares the specific code for impleme...
introduction: Slider drag verification is now use...
The usage of H tags, especially h1, has always bee...
What we are simulating now is a master-slave syst...
Table of contents Why choose react-beautiful-dnd ...
I've seen people asking before, how to add sty...
How to delete environment variables in Linux? Use...
SQL (Structured Query Language) statement, that i...
I use the simultaneous interpretation voice recog...
1. Introduction to Middleware 1. Basic concepts E...
Regarding display: flex layout, some people have ...
Table of contents Preface Main implementation cod...
Preface MRR is the abbreviation of Multi-Range Re...
To set the line spacing of <p></p>, us...
1. Import echart in HTML file <!-- Import echa...