Search code examples
nativescriptnativescript-vue

Nativescript-Vue close modal after timeout


I am opening a modal component from a Nativescript-Vue function which opens fine

this.$showModal(SuccessModal).then(() => { console.log('Modal Closed') });

I can call $modal.close from a button within the modal but getting $modal is undefined if I try to call this from, say, the mounted() hook.

I want the modal to close on its own after a three second timeout rather than the user having to click outside of the modal.

How would I go about this?


Solution

  • When using the traditional syntax for function you loose the current context (this), use arrow functions to avoid that.

    setTimeout(() => {
        this.$modal.close();
    }, 3000);
    

    Or you will have to keep reference to context in a variable

    var me = this;
    setTimeout(function() {
        me.$modal.close();
    }, 3000);