angular 参数映射_如何在Angular中使用查询参数

本文详细介绍了如何在Angular中使用查询参数,包括如何设置、保留或合并查询参数,以及如何访问这些值。示例展示了如何通过和指令传递可选参数,并利用和属性来获取当前URL中的查询参数。
摘要由CSDN通过智能技术生成

angular 参数映射

介绍 (Introduction)

Query parameters in Angular allow for passing optional parameters across any route in the application. Query parameters are different from regular route parameters, which are only available on one route and are not optional (e.g., /product/:id).

Angular中的查询参数允许跨应用程序中的任何路由传递可选参数。 查询参数与常规路由参数不同,后者仅在一条路由上可用,并且不是可选的(例如/product/:id )。

In this article, we will reference an example of an application that displays a list of products. We will provide optional order and price-range values that the receiving component can read and act on. The values provided will affect the ordering and filtering of the list of products.

在本文中,我们将引用一个显示产品列表的应用程序示例。 我们将提供可选的orderprice-range值,接收组件可以读取并对其执行操作。 提供的值将影响产品列表的排序和过滤。

Router.navigate使用查询参数 (Using Query Parameters with Router.navigate)

If you are navigating to the route imperatively using Router.navigate, you will pass in query parameters with queryParams.

如果您导航到使用势在必行路线Router.navigate ,你将通过与查询参数queryParams

In our example, if we want to route visitors to the products with the list ordered by popularity, it would look like this:

在我们的示例中,如果我们想将访问者引导至具有按受欢迎程度排序的列表的产品,则外观如下所示:

goProducts() {
  this.router.navigate(['/products'], { queryParams: { order: 'popular' } });
}

This will result in a URL that looks like this:

这将导致URL如下所示:

http://localhost:4200/products?order=popular

You can also provide multiple query parameters. In our example, if we want to route visitors to the products with the list ordered by popularity and filtered with an expensive price range, it wo

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值