Angular2 条件 switchMap 和 patchValue

Angular2 conditional switchMap and patchValue

我有一个组件可以创建或编辑行程,我想使用相同的表单。有没有参数怎么提前查看?

ngOnInit() {

    this.trip = this.fb.group({
      id: '',
      name: ['', [Validators.required, Validators.minLength(2)]],
    });

    this.route.paramMap
    .switchMap((params: ParamMap) => this.tripService.getTrip(+params.get('id')))
    .subscribe((trip: any) => {
        this.trip.patchValue({
          id: trip.id,
          name: trip.name
        });
    })
}

我已经尝试过类似的方法但没有成功

ngOnInit() {

    this.trip = this.fb.group({
      id: '',
      name: ['', [Validators.required, Validators.minLength(2)]],
    });

    this.route.paramMap
    .switchMap((params: ParamMap) => {
         console.log(params.get('id'))
      if(params.get('id')){

          this.tripService.getTrip(params.get('id')).subscribe((trip: any) => {
              this.trip.patchValue({
                id: trip.id,
                name: trip.name
              });
          })
        }

    }

    )

}

类似这样的东西怎么样:

this.route.paramMap
.filter(params => params.get('id') !== undefined)
.switchMap((params: ParamMap) => this.tripService.getTrip(+params.get('id')))
.subscribe((trip: any) => {
    this.trip.patchValue({
      id: trip.id,
      name: trip.name
    });
})

}