从ActivedRoute获取数据和参数数据

时间:2019-07-18 19:49:11

标签: angular typescript angular-routing angular-router

我当前正在使用Angular,并且我有一个网页,要求我将数据传递到另一个页面。

  1. 传递所选值(数组)列表
  2. 创建多个记录(映射到模型)

    this.activatedRoute.data.subscribe(({ model] }) => {
        setTimeout(() => {
            this.ngbModalRef = this.modalService.open(... as Component, { size: 'lg', backdrop: 'static' });
            this.ngbModalRef.componentInstance.model = model;
            this.ngbModalRef.result.then(
                result => {
                    console.log(result);
                   // this.router.navigate(['/order', { outlets: { popup: null } }]);
                    //this.ngbModalRef = null;
                },
                reason => {
                    console.log(reason);
                   // this.router.navigate(['/order', { outlets: { popup: null } }]);
                    //this.ngbModalRef = null;
                }
            );
        }, 0);
    });
    
    
    this.activatedRoute.params.subscribe((params: any) => {
        setTimeout(() => {
            this.ngbModalRef = this.modalService.open(... as Component, {
                size: 'lg',
                backdrop: 'static'
            });
            this.ngbModalRef.componentInstance.listOfNum = params['listOfNum'];
            this.ngbModalRef.result.then(
                result => {
                    this.router.navigate(['/order', { outlets: { popup: null } }], {
                        replaceUrl: true,
                        queryParamsHandling: 'merge'
                    });
                    this.ngbModalRef = null;
                },
                reason => {
                    this.router.navigate(['/order', { outlets: { popup: null } }], {
                        replaceUrl: true,
                        queryParamsHandling: 'merge'
                    });
                    this.ngbModalRef = null;
                }
            );
        }, 0);
    });
    

请告知我该怎么做才能检索这两则消息。我只能按此顺序获取模型数据。

1 个答案:

答案 0 :(得分:0)

您可以只使用rxjs forkjoin方法。

Forkjoin正在获得一个可观察的数组,并且当它们全部完成时就会触发。

forkJoin(
     this.activatedRoute.data,
     this.activatedRoute.params
).subscribe(([model, params]) => {

    this.ngbModalRef = this.modalService.open(... as Component, {
        size: 'lg',
        backdrop: 'static'
    });
    this.ngbModalRef.componentInstance.listOfNum = params['listOfNum'];
    this.ngbModalRef.componentInstance.model = model;
});

但是请记住,当两个都完成时,它会并且只有触发。