I have a child component which fetch some data from my server, before fetching I change the loading status to true and I want to set it to false after the fetch is completed. So I do something like that in my child component:
mounted() {
this.$emit('update:loadingMessage', 'Loading version options from Artifactory...');
this.$emit('update:isLoading', true);
this.fetchVersions();
},
methods: {
fetchVersions() {
const promises = [
this.$http.get(`${process.env.API_URL}/version/front`),
this.$http.get(`${process.env.API_URL}/version/back`),
];
Promise.all(promises)
.then((values) => {
// Do some stuff
})
.then(() => {
this.$emit('update:isLoading', false);
})
.catch(requestService.handleError.bind(this));
},
},
And in my parent component I listen to this event like that:
<version-selector
:form="form"
@update:loadingMessage="updateLoadingMessage"
@update:isLoading="updateLoadingStatus"
:isSnapshotVersion="isSnapshotVersion">
</version-selector>
Finally in the updateLoadingStatus
I set the isLoading
value to true
or false
accordingly.
updateLoadingMessage(message) {
this.$log.debug(message);
this.loadingMessage = message;
},
updateLoadingStatus(status) {
this.$log.debug(status);
this.isLoading = status;
},
This is useful to display or not my loading
component:
<loading
v-if="isLoading"
:loadingMessage="loadingMessage"
:isGiphy="true">
</loading>
My problem is that the first emit is working and the isLoading
value is set to true
but the second one is not working and my isLoading
value stay to true
forever... In my method updateLoadingStatus
I log the status
value and I see that this method is just called once.
this.$emit()
in a.then()
context. You could be running into issues wherethis
is referencing a different context than the Vue instance. Consider doing something likevar this_vue_instance = this;
before entering thePromise
, then usingthis_vue_instance.$emit()
instead. As for your emit capturing issues, I haven't tested this myself, but perhaps the use of the colon in your events is producing issues? I would try eliminating it as a quick test to be sure that it's not conflicting syntax. – B. Fleming.sync
in one of the recent version. Which version of Vue are you using? Edit: The attribute key having a colon could be an issue. – jostrander"vue": "^2.5.8"
– Hugo.sync
provides in version2.3.0
of Vue and later. vuejs.org/v2/guide/components.html#sync-Modifier in fact Vue itself uses the @update:key event, which may be the source of your problem. – jostranderv-on:loadingMessage="updateLoadingMessage($event)"
v-on:isLoading="updateLoadingStatus($event)"
and it didn't work neither. – Hugo