【发布时间】:2020-09-17 09:17:30
【问题描述】:
我有以下函数在vue JS中加载类别
const self = this as any;
self.fetchFeaturedCategories();
如果我控制台记录 self.fetchFeaturedCategories(),它将返回承诺。
我想实现这样的目标(这是有角度的):
this.categories = categories.map(category => {
const parentCategory = {
gender: category.gender,
id: category.id,
imageUrl: category.imageUrl,
isFeatured: category.isFeatured,
name: 'All ' + category.name,
parent: category.parent,
parentId: category.parentId,
slug: category.slug,
type: category.type,
};
category.children = [parentCategory, ...category.children];
return category;
});
但是,我想在每个孩子的数组中插入一个对象。我怎样才能做到这一点? 谢谢!
【问题讨论】:
-
假设函数 fetch... 返回一个 Promise 数组,
self.fetchFeaturedCategories().map(p=>p.then(x=>x.children['somekey']='somevalue'))) -
如果我试图将数组推送到 Promise 中,这可能吗?我刚刚编辑了我的问题,我想实现这样的目标
-
嗨,从哪里到哪里插入什么到什么?你提到了承诺,所以如果你想操纵承诺的输出,你可以在 .then 或等待它然后操纵。如果您想操作生成 Promise 的输入,这将取决于您使用的 api,并且通常不可能,因为您调用的任何函数都应该已经运行并且正在等待 I/O(Promise 代表这一点)当您尝试更改其输入时。
-
我想将父类别(如上所述)插入图像中的承诺中。
-
self.fetchFeaturedCategories() 是否返回该数组或 Promises?
标签: javascript vue.js promise vuex