Magento 2: Uncaught Error: cannot call methods on modal prior to initialization; attempted to call method 'show' when calling bootstrap modal

magento.stackexchange https://magento.stackexchange.com/questions/269644

  •  25-02-2021
  •  | 
  •  

سؤال

I am getting error when I am calling modal like this

setTimeout(function() { $('#site-pop-up').modal('show'); }, 2000);

Error in console:

Uncaught Error: cannot call methods on modal prior to initialization; attempted to call method 'show'

Anyone have solution for this?

Thanks

هل كانت مفيدة؟

المحلول

Add your code in require so that it will execute with jquery and modal is ready

require([
    'jquery',
    'Magento_Ui/js/modal/modal'
], function($){
    setTimeout(function() {
        jQuery('#site-pop-up').modal('show');
    }, 2000);
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top