为什么每次我向数组添加一个元素时它都会为我更改所有值?
Why does it change all values for me every time I add an element to the array?
每次我添加一个元素时,它都会自动替换所有其他元素,但只有当我插入整个对象时才会发生这种情况如果我插入一个经典元素,一个整数,一个字符串可以正常工作。
<body>
<div id="app">
<button @click="post()">POST</button>
<h1>{{ar.name}} {{ar.surname}} {{ar.gender}}</h1>
<br />
<hr />
<div v-for="(value, index) in array">
<text>{{index}} {{value}} </text>
</div>
</div>
<script src="https://unpkg.com/vue@3"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
let app = Vue.createApp({
data: function () {
return {
ar: {
name: "",
surname: "",
gender: "",
},
array: [],
};
},
methods: {
async post() {
await axios
.get("https://randomuser.me/api/")
.then((r) => {
arr = r.data.results;
arr.forEach((element) => {
this.ar.name = element.name.first;
this.ar.surname = element.name.last;
this.ar.gender = element.gender;
this.array.push(this.ar);
});
})
.catch((err) => {
console.log("Error " + err);
});
},
},
});
app.mount("#app");
</script>
</body>
</html>
first element insert
second element insert
您可以在迭代内的对象中分配响应并在 this.ar
中传递它。试试这个 :
const URL = "https://randomuser.me/api/";
new Vue({
el: '#app',
data() {
return {
ar: {
name: "",
surname: "",
gender: "",
},
userArray: [],
}
},
methods: {
post() {
axios.get(URL).then((r) => {
arr = r.data.results;
arr.forEach((element) => {
let obj = {};
obj.name = element.name.first;
obj.surname = element.name.last;
obj.gender = element.gender;
this.ar = obj;
this.userArray.push(obj);
});
})
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>
<div id="app">
<button @click="post()">POST</button>
<h1>{{ar.name}} {{ar.surname}} {{ar.gender}}</h1>
<br />
<hr />
<div v-for="(value, index) in userArray" :key="index">
<span> {{index}} {{value}} </span>
</div>
</div>
您遇到了指针引用问题。您实际上多次将相同的对象指针添加到数组,即相同的对象引用:
问题说明
const obj = { name: 'ref1' };
const array = [];
array.push(obj);
array.push(obj);
array.push(obj);
// array === [{ name: 'ref1' }, { name: 'ref1' }, { name: 'ref1' }]
// which is actually [$objRef1, $objRef1, $objRef1] where $objRef1 is a pointer to the object address.
// when you updates the object,
obj.name = 'Hello'
// you still have array = [$objRef1, $objRef1, $objRef1] where $objRef1 all referes to this object: { name: 'Hello' }
// array === [{ name: 'Hello' }, { name: 'Hello' }, { name: 'Hello' }]
解决方案:
您必须在每次迭代时创建一个新对象,因此它们都是具有不同地址(指针)的不同对象。
await axios
.get("https://randomuser.me/api/")
.then((r) => {
const arr = r.data.results;
arr.forEach((element) => {
const item = {
name: element.name.first,
surname: element.name.last,
gender: element.gender,
};
this.ar = item // refers to the new created item
this.array.push(item);
});
}).catch((err) => {
console.log("Error " + err);
});
每次我添加一个元素时,它都会自动替换所有其他元素,但只有当我插入整个对象时才会发生这种情况如果我插入一个经典元素,一个整数,一个字符串可以正常工作。
<body>
<div id="app">
<button @click="post()">POST</button>
<h1>{{ar.name}} {{ar.surname}} {{ar.gender}}</h1>
<br />
<hr />
<div v-for="(value, index) in array">
<text>{{index}} {{value}} </text>
</div>
</div>
<script src="https://unpkg.com/vue@3"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
let app = Vue.createApp({
data: function () {
return {
ar: {
name: "",
surname: "",
gender: "",
},
array: [],
};
},
methods: {
async post() {
await axios
.get("https://randomuser.me/api/")
.then((r) => {
arr = r.data.results;
arr.forEach((element) => {
this.ar.name = element.name.first;
this.ar.surname = element.name.last;
this.ar.gender = element.gender;
this.array.push(this.ar);
});
})
.catch((err) => {
console.log("Error " + err);
});
},
},
});
app.mount("#app");
</script>
</body>
</html>
first element insert
second element insert
您可以在迭代内的对象中分配响应并在 this.ar
中传递它。试试这个 :
const URL = "https://randomuser.me/api/";
new Vue({
el: '#app',
data() {
return {
ar: {
name: "",
surname: "",
gender: "",
},
userArray: [],
}
},
methods: {
post() {
axios.get(URL).then((r) => {
arr = r.data.results;
arr.forEach((element) => {
let obj = {};
obj.name = element.name.first;
obj.surname = element.name.last;
obj.gender = element.gender;
this.ar = obj;
this.userArray.push(obj);
});
})
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.17.1/axios.js"></script>
<div id="app">
<button @click="post()">POST</button>
<h1>{{ar.name}} {{ar.surname}} {{ar.gender}}</h1>
<br />
<hr />
<div v-for="(value, index) in userArray" :key="index">
<span> {{index}} {{value}} </span>
</div>
</div>
您遇到了指针引用问题。您实际上多次将相同的对象指针添加到数组,即相同的对象引用:
问题说明
const obj = { name: 'ref1' };
const array = [];
array.push(obj);
array.push(obj);
array.push(obj);
// array === [{ name: 'ref1' }, { name: 'ref1' }, { name: 'ref1' }]
// which is actually [$objRef1, $objRef1, $objRef1] where $objRef1 is a pointer to the object address.
// when you updates the object,
obj.name = 'Hello'
// you still have array = [$objRef1, $objRef1, $objRef1] where $objRef1 all referes to this object: { name: 'Hello' }
// array === [{ name: 'Hello' }, { name: 'Hello' }, { name: 'Hello' }]
解决方案:
您必须在每次迭代时创建一个新对象,因此它们都是具有不同地址(指针)的不同对象。
await axios
.get("https://randomuser.me/api/")
.then((r) => {
const arr = r.data.results;
arr.forEach((element) => {
const item = {
name: element.name.first,
surname: element.name.last,
gender: element.gender,
};
this.ar = item // refers to the new created item
this.array.push(item);
});
}).catch((err) => {
console.log("Error " + err);
});