I'm currently in the process of developing a shopping cart feature using Laravel and Vue. In my system, when an item is added to the shopping basket, I want to display a confirmation message by toggling a Vue variable that is being monitored through a v-if directive:
<div class="alert alert-success" v-if="basketAddSuccess" transition="expand">Added to the basket</div>
Here's the relevant JavaScript code snippet:
addToBasket: function(){
item = this.product;
this.$http.post('/api/buy/addToBasket', item);
this.basketAddSuccess = true;
}
(I will be implementing error handling with then-catch soon).
While the message displays correctly, I want it to disappear after a specific duration, say a few seconds. How can achieve this behavior using Vue? I attempted using setTimeOut
, but Vue raised an error indicating that it's undefined.
EDIT: I had been misspelling setTimeout
; however, even after correcting it, the solution still doesn't work as expected:
The updated function looks like this:
addToBasket: function(){
item = this.photo;
this.$http.post('/api/buy/addToBasket', item);
this.basketAddSuccess = true;
setTimeout(function(){
this.basketAddSuccess = false;
}, 2000);
}