【发布时间】:2017-05-20 07:13:43
【问题描述】:
我需要从对象 this.props.payload 中获取 3 个变量 cx、cx 和 weatherIcon,目前我正在使用此代码
const { cx, cy } = this.props
const {weatherIcon} = this.props.payload
有效,但我想知道是否可以写在一行中。
【问题讨论】:
我需要从对象 this.props.payload 中获取 3 个变量 cx、cx 和 weatherIcon,目前我正在使用此代码
const { cx, cy } = this.props
const {weatherIcon} = this.props.payload
有效,但我想知道是否可以写在一行中。
【问题讨论】:
试试这个。
const { cx, cy, payload: { weatherIcon } } = this.props;
const props = { cx: 1, cy: 2, payload: { weatherIcon: 3 }};
const { cx, cy, payload: { weatherIcon } } = props;
console.log(cx, cy, weatherIcon);
【讨论】: