【问题标题】:How to define queryParams Angular 5 router如何定义queryParams Angular 5路由器
【发布时间】:2018-05-08 09:55:09
【问题描述】:

我对查询参数有疑问。每个路由器都有一些不同的查询参数。

$stateProvider 允许在 $state 中定义 queryParams

.state('contacts.list', {
    templateUrl: 'contacts.list.html',
    url: '/test?test1&test2'
    data: {
        customData1: 44,
        customData2: "red"
    } 
  })

此代码只允许从 queryParams 中获取 test1 和 test2 字段。

关于新的 Angular 路由器,如果我需要观看 queryParams,我应该订阅

this.activatedRoute.queryParams.subscribe(params => {

});

这段代码有问题,如果我在 url 中输入一些第三方 test3=lol,我会在参数 params 中得到这个参数,我不想在所有地方过滤它

如何在 Angular 5 路由器中设置 '/test?test1&test2'

【问题讨论】:

    标签: angular angular-router


    【解决方案1】:
    goProducts() {
      this.router.navigate(['/products'], { queryParams: { order: 'popular', 'price-range': 'not-cheap' } });
    }
    

    而且网址会是这样的。

    http://localhost:4200/products?order=popular&price-range=not-cheap

    在你的组件构造函数中

    this.activatedRoute.queryParams.subscribe(params => {
    
    });
    

    希望这会有所帮助。使用this 回答您的问题。

    【讨论】:

    • 如果我使用这种方法,我会遇到这样的情况:用户输入一些第三方参数到 url “test=lol”,然后按回车 我的订阅将处理这个变化,我会得到这个意想不到的参数“test”在我的处理程序中
    • 那么您必须在不使用查询参数的情况下将数据发送到该组件。如果您需要对用户隐藏数据,则必须以另一种方式执行此操作。使用服务来执行此操作。
    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多