首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Angular中将routerLink中的Date对象作为参数传递

如何在Angular中将routerLink中的Date对象作为参数传递
EN

Stack Overflow用户
提问于 2020-07-06 23:36:06
回答 3查看 954关注 0票数 0

这是我的代码日期,在这里我试图将checkinFrom block.In对象作为routerLink中的参数传递给另一个组件。

代码语言:javascript
运行
复制
<app-chart-chip [routerLink]="['/statistics/subsegment',currentPS,checkinFrom]"
                                   [currentPS]="currentPS" [fromDate]="checkinFrom">
            </app-chart-chip>

这是我的父组件代码。

代码语言:javascript
运行
复制
 currentPS: string;
    checkinFrom = new Date(2019, 8, 1);
    ngOnInit() {
     this.currentPS = 'PS';
    
    }

这是子组件代码

代码语言:javascript
运行
复制
export class ChartChipComponent implements OnInit, OnChanges{
     @Input() fromDate = new Date();
     @Input() currentPS: string;
}

这是app.routing.module.ts文件

代码语言:javascript
运行
复制
{
    path: 'statistics/subsegment/:currentPS/:fromDate',
    component: StatisticsComponent,
    loadChildren: 'src/app/statistics/statistics.module#StatisticsModule',
    data: {
      title: 'Subsegmentation'
    }
  }

对于这个组件,我尝试在URL的routerLink中获取日期作为参数。

代码语言:javascript
运行
复制
export class StatisticsComponent implements OnInit {

 ngOnInit() {
   this.getParticularId();
}
getParticularId() {
    this.route.params.subscribe( params => {
    console.log(params);
}
}

但它正在重定向到仪表板。

代码语言:javascript
运行
复制
{
    path: '**',
    redirectTo: 'dashboard',
    pathMatch: 'full'
  }
EN

回答 3

Stack Overflow用户

发布于 2020-07-06 23:50:26

在app图表芯片中添加以下代码

[queryParams]="{date:checkinFrom }"

代码语言:javascript
运行
复制
<app-chart-chip [routerLink]="['/statistics/subsegment',currentPS,checkinFrom]"
     [queryParams]="{date:checkinFrom }"                              [currentPS]="currentPS" [fromDate]="checkinFrom">
            </app-chart-chip>

你可以像这样得到日期

代码语言:javascript
运行
复制
new URLSearchParams(new URL('http://localhost:4400/dashboard?date=Sun%20Sep%2001%202019%2000:00:00%20GMT%2B0530%20(India%20Standard%20Time').search).get('date');

输出"Sun Sep 01 2019 00:00:00 GMT+0530 (India Standard Time"

票数 0
EN

Stack Overflow用户

发布于 2020-07-06 23:51:03

你传递日期的方式很好,但我认为routerLink就是错了。

试试这个:

代码语言:javascript
运行
复制
  constructor(private route: ActivatedRoute) { }

  this.route.paramMap.subscribe(params => {
    console.log(params)
  })

HTML:

代码语言:javascript
运行
复制
  [routerLink]="['/', 'statistics', 'subsegment', currentPS,checkinFrom]"
票数 0
EN

Stack Overflow用户

发布于 2021-10-03 09:56:01

试试这个:

代码语言:javascript
运行
复制
[routerLink]="['/consultation/calendar/']"
[queryParams]="{ date: lastAppointment?.startDate }"

然后:

代码语言:javascript
运行
复制
this.route.snapshot.queryParams['date']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62759466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档