【问题标题】:Passing query parameter by using navigateByUrl()通过使用 navigateByUrl() 传递查询参数
【发布时间】:2020-01-01 16:49:16
【问题描述】:

我正在尝试通过单击图标导航到新页面,下面是代码

  this.prj = e.data.project_number;
  this.router.navigateByUrl('/dashboard/ProjectShipment/634');

我必须将this.prj 传递给它,而不是这个硬编码的查询参数000634。我的路径如下所示

const appRoutes: Routes = [
  {
  path: 'dB',
  data: { title: 'Dashboard' },
  children: [
      {
          path: 'ProjectShipment/:reportProject',
          component: ProjectShipmentComponent,
          data: { title: 'Project Shipment' },
      }

【问题讨论】:

  • 我认为您在谈论 PATH 参数,而不是 QUERY 参数。如果您尝试传递路径参数,MuruGan 的答案是正确的,并且 url 看起来像“/dashboard/ProjectShipment/634”。但如果我们谈论的是查询参数,那么 Miguel Pinto 的答案是正确的,并且 url 看起来像 '/dashboard/ProjectShipment?prj=634'

标签: javascript angular typescript angular7 angular-routing


【解决方案1】:

this.router.navigate(['/route1'], { variable1: "Value" }); this.router.navigateByUrl('/route1', { variable1: "Value" });

请不要:如果您的网址是urlencoded,请使用navigateByUrl,如果您的网址不是urlencoded,请使用navigate

【讨论】:

    【解决方案2】:

    简单地使用字符串插值

    this.router.navigateByUrl(`/dashboard/ProjectShipment/${this.prj}`);
    

    【讨论】:

    • 如果 prj 是一个对象呢?
    • 我们可以指定什么 route.module.ts
    【解决方案3】:

    您可以使用“router.navigate”代替“router.navigateByUrl”:

    this.router.navigate([URL],{ queryParams: { id: this.prj });
    

    【讨论】:

      【解决方案4】:
      // Set our navigation extras object
      // that passes on our global query params and fragment
      let navigationExtras: NavigationExtras = {
        queryParams: {...},
        state: {...}
      };
      
      // Redirect the user
      this.router.navigateByUrl(redirect, navigationExtras);
      

      NavigationExtras docs

      编辑:https://stackoverflow.com/a/44865817/5011818 这也值得一看

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多