I'm working with Vuejs and have 3 images, a schedule determines which image displays. The appropriate src is loaded in the Computed section so that the images cycle reactively, but it still won't change image unless I manually refresh.
<template>
<div class="mainDisplay">
<!--Image div for morning schedule-->
<div class="imgContainer" v-if="getSchedule(currentHour()).includes('s1')">
<img class="banner" :src="getSrc" alt="Morning Cappucino">
</div>
<!--Image div for afternoon schedule-->
<div class="imgContainer" v-else-if="getSchedule(currentHour()).includes('s2')">
<img class="banner" :src="getSrc" alt="Afternoon Latte">
</div>
<!--Image div for evening schedule-->
<div class="imgContainer" v-else-if="getSchedule(currentHour()).includes('s3')">
<img class="banner" :src="getSrc" alt="Evening Hot Chocolate">
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
morningCaption: " ",
afternoonCaption: " ",
eveningCaption: " ",
morningSrc: "./public",
afternoonSrc: "./public",
eveningSrc: "./public",
schedule: "",
src: "",
}
},
mounted() {
//xml file calling
axios.get("/screen-schedule-config.xml")
.then(response => {
//parse xml data
let xmlobj = response.data;
let domParser = new DOMParser();
let xmlDocument = domParser.parseFromString(xmlobj, "text/xml");
// set image path
let imagePath = xmlDocument.getElementsByTagName("assets");
//set image src paths
this.morningSrc = imagePath[0].querySelector("asset").getAttribute("path");
this.afternoonSrc = imagePath[0].querySelectorAll("asset")[1].getAttribute("path");
this.eveningSrc = imagePath[0].querySelectorAll("asset")[2].getAttribute("path");
}
)
},
methods: {
//get current hour
currentHour() {
const currentDate = new Date();
const hour = currentDate.getHours();
return hour
},
//get schedule for sequence
getSchedule(hour) {
let schedule = "";
if (hour >= 0 && hour < 12) {
schedule = "s1";
} else if (hour >= 12 && hour < 19) {
schedule = "s2";
} else {
schedule = "s3";
}
return this.schedule = schedule
}
},
// computed property allows you to have logic that is dependent on reactive data.
computed: {
getSrc() {
if (this.schedule.includes("s1")) {
this.src = this.morningSrc
} else if (this.schedule.includes("s2")) {
this.src = this.afternoonSrc
} else if (this.schedule.includes("s3")) {
this.src = this.eveningSrc
}
return this.src
}
}
}
</script>
My image paths come from the public folder and I have an inkling it's to do with this. Thank you!