使用navigateByUrl()传递查询参数

问题描述 投票:0回答:4

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

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

我必须向其中传递一个

000634
,而不是这个硬编码的查询参数
this.prj
。我的路径如下

const appRoutes: Routes = [
  {
  path: 'dB',
  data: { title: 'Dashboard' },
  children: [
      {
          path: 'ProjectShipment/:reportProject',
          component: ProjectShipmentComponent,
          data: { title: 'Project Shipment' },
      }
javascript angular typescript angular7 angular-routing
4个回答
15
投票

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

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

7
投票

简单地使用字符串插值

this.router.navigateByUrl(`/dashboard/ProjectShipment/${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 文档

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


-2
投票

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

请不要:如果您的网址是

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

© www.soinside.com 2019 - 2024. All rights reserved.