简体   繁体   English

在 vue(vue-router) 中更新查询参数而不重新加载页面

[英]Update query params in vue(vue-router) without reloading page

I have a page that has a table that has Pagination and tabs .我有一个页面,其中有一个包含Paginationtabs的表格。 Url Looks something like this. Url 看起来像这样。

http://localhost:4000/listPage?tab=all&page=1&items=10 http://localhost:4000/listPage?tab=all&page=1&items=10

So I want to update the URL state(query params) every time user paginates and changes tabs without reloading the page only making requests to API's.所以我想在每次用户分页和更改选项卡时更新 URL 状态(查询参数)而不重新加载页面只向 API 发出请求。

Same for changing the tab.更改选项卡也是如此。 Update the query params without reloading the page.在不重新加载页面的情况下更新查询参数。

I have tried using the history.pushState but this will mess with vue-router history and clicking back and forward will no longer take you to the correct page.我曾尝试使用history.pushState但这会与 vue-router 历史记录混淆,并且单击后退和前进将不再将您带到正确的页面。

history.pushState(
            {},
            null,
            this.$route.path + '?'
            + 'view=' + this.currentView + '&'
            + 'page=' + this.currentPage + '&'
            + 'items=' + this.items
          );

This should be enough to solve your case这应该足以解决您的问题

<button 
  @click="$router.replace({ 
    path: $route.path,
    query: { tab: 'all', page: 1, items: 10 } 
  })"
>
  paginate without moving
</button>

More info available here: https://router.vuejs.org/guide/essentials/navigation.html#navigate-to-a-different-location此处提供更多信息: https://router.vuejs.org/guide/essentials/navigation.html#navigate-to-a-different-location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM