Я играю с vue js и сторонним API.Мне удалось получить данные JSON и представить их в моем HTML, но я борюсь с изображениями.Некоторые изображения отсутствуют в файле json, поэтому я хранил их локально на своем ноутбуке.
Я попытался установить пустой источник изображений, используя v-if в моем html, но безуспешно.(см. комментарии в моем html-файле)
Также я попытался назначить класс для каждого img, а затем попытался установить источник img, используя jquery $("#zTTWa2").attr("src", "missing_beers-logo/11.5%20plato.png");
, но также безуспешно.
Где моя вина?Возможно, мой подход совершенно неверен, потому что я новичок в кодировании, и любое предложение будет оценено.Заранее спасибо
var app = new Vue({
el: "#app",
data: {
beers: [],
decrArray: [], //array with img links
cleanedArray: [], //without undefined
path: 0,
images: [missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png",
"missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png",
"missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png", "missing_beers-logo/11.5%20plato.png"],
created: function() {
this.getData();
},
methods: {
getData: function() {
var fetchConfig =
fetch("http://api.brewerydb.com/v2/beers?key=6a3ac324d48edac474417bab5926b70b&format=json", {
method: "GET",
dataType: 'jsonp',
// responseType:'application/json',
// "Content-Type": 'application/json',
headers: new Headers({
"X-API-Key": '6a3ac324d48edac474417bab5926b70b',
'Content-Type': 'application/json',
"Access-Control-Allow-Credentials": true,
"Access-Control-Allow-Origin": '*',
"Access-Control-Allow-Methods": 'GET',
"Access-Control-Allow-Headers": 'application/json',
})
}).then(function(response) {
if (response.ok) {
return response.json();
}
}).then(function(json) {
console.log("My json", json)
// console.log("hi");
app.beers = json.data;
console.log(app.beers);
app.pushDescr();
console.log(app.decrArray);
app.removeUndef();
// console.log(app.cleanedArray);
})
.catch(function(error) {
console.log(error);
})
},
pushDescr: function() {
console.log("hi");
for (var i = 0; i < app.beers.length; i++) {
app.decrArray.push(app.beers[i].labels);
}
return app.decrArray;
},
removeUndef: function() {
for (var i = 0; i < app.decrArray.length; i++) {
if (app.decrArray[i] === undefined) {
app.decrArray[i] = "";
}
}
console.log(app.decrArray);
},
getMissingImg(index){
return(this.images[index]);
},
}
})
<div class="app">
<div v-for="(value, index) in beers">
{{value.name}}
<!--
<img v-bind:src="decrArray[index].medium" :class="value.id"/>
-->
<div v-if="decrArray[index].medium !==undefined ">
<img :src="decrArray[index].medium" />
</div>
<div v-else>
<img :src="getMissingImg(index)" />
</div>
</div>
</div>