javascript - axios GET,更新Image Url vue JS
问题描述
调用 Axios get 方法后,我的网页收到了一些图像 URL 的响应,但我无法单独更新/刷新/重新渲染图像而不重新渲染整个页面。谁能帮我解决这个问题?示例将不胜感激。
这是我的代码
<template>
img class="ig-img" src="arrayInstagramRequest[0]" alt="image">
</template>
data: function () {
return {
arrayInstagramRequest: [],
convertedArrayInstagram: [],
id: 0
}
},
httpGetIstagramImage: function () {
axios.get(configFile.basic_url + '/instagram_content/get_all').then((response) => {
while (response.data.result[this.id] != null) {
this.arrayInstagramRequest.push(response.data.result[this.id].instagram_url)
this.id += 1
}
console.log(this.id)
console.log(this.arrayInstagramRequest[0])
}).catch((error) => {
console.log(error)
})
}
解决方案
第1步:template
会像
<template>
<div id="app">
<img alt="Instagram Image" :src="arrayInstagramRequest[0]" width="25%" />
</div>
</template>
第2步:axios
我模拟了数据对象中的响应,而不是响应dummyResponse
,数据将在下面,
data() {
return {
arrayInstagramRequest: [],
convertedArrayInstagram: [],
id: 0,
dummyResponse: [
{
id: 0,
Name: "Spec_0",
Image: "https://www.encodedna.com/images/theme/jQuery.png",
},
{
id: 1,
Name: "Spec_1",
Image: "https://www.encodedna.com/images/theme/json.png",
},
{
id: 2,
Name: "Spec_2",
Image: "https://www.encodedna.com/images/theme/angularjs.png",
},
],
};
},
第 3 步:我不是从我httpGetIstagramImage
那里获取响应,而是从图像对象中获取并绑定到图像对象。axios
dummyResponse
methods: {
httpGetIstagramImage: function () {
// axios
// .get("configFile.basic_url/instagram_content/get_all")
// .then((response) => {
// while (response.data.result[this.id] != null) {
// this.arrayInstagramRequest.push(
// response.data.result[this.id].instagram_url
// );
// this.id += 1;
// }
// console.log(this.id);
// console.log(this.arrayInstagramRequest[0]);
// })
// .catch((error) => {
// console.log(error);
// });
if (this.dummyResponse) {
this.dummyResponse.filter(function (e) {
if (e.id === this.id) {
this.arrayInstagramRequest.push(e.Image);
}
return e.id === this.id;
}, this);
}
},
},
第 4 步:我正在调用该函数的 OnCreate 方法httpGetIstagramImage
created() {
this.httpGetIstagramImage();
},
推荐阅读
- c++ - findChessboardCorners() 无法检测背光条件下的角点
- python - 为什么while循环只循环if语句的一部分?
- php - phpcurl 无法放入 Amazon s3
- javascript - react-datepicker,禁用特定日期的问题
- symfony - 在 Where IN 上使用带有多个 addSelect 的子查询
- php - 在 PHP 中查找 JSON 中的键的路径
- node.js - 在 Mongoose 中使用 ISODate 的正确方法是什么
- firebase - Firebase CLI:如何在 POST 请求中发送正文?
- c++ - 序列锁中的 atomic_thread_fence 要求
- python - Python作业涉及将字符串中的字符转换为数字