我目前正在开发一个 Vue 2 项目,我必须从异步函式 scoop 外部推送一个物件。这是下面主题中的代码:
async getData() {
return [
{
id: "1",
name: "Slim",
age: 5,
location: 1500,
breed: 500,
},
{
id: "2",
name: "Sol",
age: 3,
location: 1500,
breed: 1,
},
};
有没有办法从 getData() 勺子外部访问和推送这个物件阵列的资料
uj5u.com热心网友回复:
我建议您在函式范围之外宣告一个阵列,然后对其进行更改,或者您可以将更新阵列作为自变量传递。
let array = [
{
id: "1",
name: "Slim",
age: 5,
location: 1500,
breed: 500,
},
{
id: "2",
name: "Sol",
age: 3,
location: 1500,
breed: 1,
}];
//pass array as a parameter
async getData(array){
return array
}
//update your array
array.push("your object");
//pass updated array in function call
getData(array);
uj5u.com热心网友回复:
您也可以使用这种方法,
async function getData(item) {
return [
{
id: "1",
name: "Slim",
age: 5,
location: 1500,
breed: 500,
},
{
id: "2",
name: "Sol",
age: 3,
location: 1500,
breed: 1,
},
item
]
};
getData( {
id: "3",
name: "obj",
age: 5,
location: 1500,
breed: 1,
});
0 评论