简体   繁体   English

Angular 9条动态路由

[英]Angular 9 dynamic routes

first of all sorry for my english i dont speak very well but I will try to explain myself, I'm creating an eCommerce in Angular 9 and the routes of this eCommerce work as follows, every route that comes in the end with .html I direct to a component that I call router-redirects, when I fall into that component I call the ecommerce api passing the url in question then it returns if that url is a category, a product, an article.. with this answer I redirect to the right component.首先抱歉我的英语不太好,但我会试着解释一下自己,我正在 Angular 9 中创建一个电子商务,这个电子商务的路线如下,最后的每条路线都是.html I直接到我称为路由器重定向的组件,当我落入该组件时,我将电子商务称为 api 传递有问题的 url 然后它返回如果该 Z572D4E421E5E6B9BC11D815E8A021 是一个重定向的产品类别。正确的组件。

The problem is that in certain situations like the categories page I need to leave some query strings like pageIndex and orderby in the URL but angular does not show the query strings because when I do the navigate I put skipLocationChange: true because I cannot change the url and I need to keep the queryString. The problem is that in certain situations like the categories page I need to leave some query strings like pageIndex and orderby in the URL but angular does not show the query strings because when I do the navigate I put skipLocationChange: true because I cannot change the url我需要保留查询字符串。

I only need this for when a user wants to share the url with someone else.我只在用户想与其他人共享 url 时才需要它。

Example of how I am doing the navigate method:我如何执行导航方法的示例:

this.router.navigate(['categoria-listagem'], {
      skipLocationChange: true,
      queryParams: { page: 1, order: 'novidades' }
});

Please if anyone knows a better way to do this redirection or know how to keep query strings even with skipLocationChange it would save me !!请如果有人知道执行此重定向的更好方法或知道如何保留查询字符串,即使使用 skipLocationChange 也会救我!

Thank you in advance.先感谢您。

It sounds like you should add queryParamsHandling as 'merge' or 'preserve' (depending on your requirements) eg听起来您应该将queryParamsHandling添加为“合并”或“保留”(取决于您的要求),例如

this.router.navigate(['categoria-listagem'], {
  skipLocationChange: true,
  queryParams: { page: 1, order: 'novidades' },  
  queryParamsHandling: "merge"
});

This should add your new query params to any already in your URL.这应该将您的新查询参数添加到 URL 中的任何已有参数中。

Also you may need to remove skipLocationChange , again depending on the functionality you need.此外,您可能需要再次删除skipLocationChange ,具体取决于您需要的功能。

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

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