如果id为null,我希望变量x为true。我会使用if和else,但在管道中不能这样做。请帮帮我。
private x = false;
private y = false;
ngOnInit() {
this.subscribe = this.route.params.pipe(
map(({ id }) => id),
filter(id => !!id), // <---- here
switchMap((id: string) =>
this.shippingService.getShippingById(id)))
.subscribe(
res => {
this.shippingData = res;
this.y= true;
},
err => this.error = err.error,
);
}
您可以在过滤器之前使用tap
运算符:
ngOnInit() {
this.subscribe = this.route.params.pipe(
map(({ id }) => id),
tap((val) => { if (val === null) this.x = true }),
filter(id => !!id),
switchMap((id: string) =>
this.shippingService.getShippingById(id)))
.subscribe(
res => {
this.shippingData = res;
this.y= true;
},
err => this.error = err.error,
);
}