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

The role of query and params in vue

下次还敢
Release: 2024-05-02 21:12:50
Original
454 people have browsed it

In Vue routing, query is used to pass additional information that does not affect the page route, such as /products?page=2&sort=price; params is used to define the parameter part of the page route, such as /products/:id.

The role of query and params in vue

The role of query and params in Vue

In Vue routing, query and params Play different roles and serve different purposes.

query

  • Function: Pass additional information in the URL that does not affect the page route itself.
  • Syntax: this.$route.query[parameter name]
  • Usage: Usually used to pass filters , paging, or searching for information, for example:
<code>/products?page=2&sort=price</code>
Copy after login

In the component, these additional parameters can be accessed using the query object:

<code class="javascript">export default {
  data() {
    return {
      currentPage: this.$route.query.page,
      sortOrder: this.$route.query.sort,
    }
  }
}</code>
Copy after login

params

  • Function: Define the parameter part of a specific page route, which is used to match and render the corresponding components.
  • Syntax: this.$route.params[parameter name]
  • Usage: params Used to extract dynamic parameters from the URL path, for example:
<code>/products/:id</code>
Copy after login

In the component, you can use the params object to access these dynamic parameters:

<code class="javascript">export default {
  data() {
    return {
      productId: this.$route.params.id,
    }
  }
}</code>
Copy after login

Summary

query and params have different purposes in Vue routing. query is used to pass additional information, while params is used to define the parameter portion of the page route. By understanding their purpose, you can effectively manage URLs and routes in your Vue application.

The above is the detailed content of The role of query and params in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
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 admin@php.cn
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!