是否有ES6速记方式将var设置为等于函数中的数据?

时间:2017-01-28 05:34:15

标签: javascript ecmascript-6

例如我有这个。

api.get('show/?populate=author&limit=100').then(function(response) {
    vm.shows = response.data;
}).catch(function(error) {
    vm.error = `The API isn't responding.`;
    throw new Error(error);
});

我可以使用这样的东西吗?

api.get('show/?populate=author&limit=100').then(response.data => vm.shows);

1 个答案:

答案 0 :(得分:3)

您无法立即分配给变量,但可以使用destructuring速记response.data。例如:

// data becomes the "data" property of the callback's first argument
api.get('show/?populate=author&limit=100').then(({data}) => vm.shows = data);
相关问题