在 Rxjs 的 map 运算符中,body 在花括号内和 body 在圆括号内代表什么?
What does body represent inside curly brackets and body inside round brackets in map operator in Rxjs?
我在应用程序中有 2 个 API 调用。我在一次调用中看到地图运算符中大括号内的 body 例如 map(({ body }: any) => {this.assembleOrder(body.data)}) 和 body 在第二次调用中地图运算符的圆括号内,例如 map((body: any) => this.assembleOrder(body.data))。它们有什么区别?
代码:
public getOrder$(orderID: string): Observable<OrderModel[]> {
return this.http
.get(`${this.Url}?orderID=${orderID}`,
{ observe: 'response' }
)
.pipe(
map(({ body }: any) => {this.assembleOrder(body.data)})
);
}
public upgradeOrder$(Order: OrderModel): Observable<OrderModel> {
return this.http
.post(`${this.Url}deals/copy/${Order.id}/upgrade`, null)
.pipe(
map((body: any) => this.assembleOrder(body.data))
);
}
您看到的语法称为 Object Destructuring。它是一种 shorthand 语法,用于分配来自对象属性的变量。
这是一个从“用户”对象定义 3 个常量的示例:
const id = user.id;
const name = user.name;
const age = user.age;
使用对象解构,这可以在一行中实现:
const { id, name, age } = user;
在你没有大括号的例子中:
map((body: any) => this.assembleOrder(body.data))
没有解构,所以 body
参数只是从您的 http 调用中获取响应。
在带大括号的例子中:
map(({ body }: any) => {this.assembleOrder(body.data)})
使用了解构,所以 body
从 http 响应的正文 属性 接收值。
没有解构,它看起来像:
map((response: any) => {this.assembleOrder(response.body.data)})
我在应用程序中有 2 个 API 调用。我在一次调用中看到地图运算符中大括号内的 body 例如 map(({ body }: any) => {this.assembleOrder(body.data)}) 和 body 在第二次调用中地图运算符的圆括号内,例如 map((body: any) => this.assembleOrder(body.data))。它们有什么区别?
代码:
public getOrder$(orderID: string): Observable<OrderModel[]> {
return this.http
.get(`${this.Url}?orderID=${orderID}`,
{ observe: 'response' }
)
.pipe(
map(({ body }: any) => {this.assembleOrder(body.data)})
);
}
public upgradeOrder$(Order: OrderModel): Observable<OrderModel> {
return this.http
.post(`${this.Url}deals/copy/${Order.id}/upgrade`, null)
.pipe(
map((body: any) => this.assembleOrder(body.data))
);
}
您看到的语法称为 Object Destructuring。它是一种 shorthand 语法,用于分配来自对象属性的变量。
这是一个从“用户”对象定义 3 个常量的示例:
const id = user.id;
const name = user.name;
const age = user.age;
使用对象解构,这可以在一行中实现:
const { id, name, age } = user;
在你没有大括号的例子中:
map((body: any) => this.assembleOrder(body.data))
没有解构,所以 body
参数只是从您的 http 调用中获取响应。
在带大括号的例子中:
map(({ body }: any) => {this.assembleOrder(body.data)})
使用了解构,所以 body
从 http 响应的正文 属性 接收值。
没有解构,它看起来像:
map((response: any) => {this.assembleOrder(response.body.data)})