In my application, I encountered a strange issue where a h3
tag containing a title bound to a reactive data property from a Firestore database would sometimes not display properly after a page reload. Oddly enough, when accessing the page through client-side navigation, everything worked fine.
Here is the relevant code snippet:
<template>
<h3 @click="displayCoursePreview" class="mt-5">{{ titl }}</h3>
</template>
<script>
props: {
student: {
type: Boolean
}
},
watch: {
rehydrated: {
// Always triggers once store data is rehydrated (seems to work without any problems)
immediate: true,
async handler(newVal, oldVal) {
if (newVal) {
await this.getSections();
return this.getTopics();
}
}
}
},
data() {
return {
titl: null
};
},
computed: {
rehydrated() {
return this.$store.state.rehydrated; // Equals true once store is rehydrated from local storage
}
},
methods: {
getSections() {
console.log('running') // Runs every time
let ref = this.$store.state.courses;
var cid = this.student
? ref.currentlyStudying.cid
: ref.currentlyPreviewing.cid;
// Get Course Title
this.$fireStore
.collection("courses")
.doc(cid)
.get()
.then(doc => {
console.log(doc.data().name) // Logs correct title every time
this.titl = doc.data().name;
this.thumbSrc = doc.data().imgsrc;
})
.catch(err => console.log(err));
}
</script>
I'm still puzzled as to why the title does not always display correctly. Is there an alternative way to bind titl
to the content of the h3
tag without using the {{}}
syntax?
Thank you for any insights!
EDIT:
Update on the issue - I changed the {{}}
syntax to v-text
, like this:
<h3 @click="displayCoursePreview" class="mt-5" v-text="titl"></h3>
And now it consistently works, even after a hard reload. Can anyone shed light on why this change made a difference?