Skip to content
Advertisement

How to use defer and make JS plugin working as well

When I use defer on app.js, there is a js plugin not working properly.

If I remove defer, although the plugin works, but there is a warning to ask me to use defer, and I don’t know what to do.

JavaScript
JavaScript

This is the test.blade.php

JavaScript

If I use <script src="{{ asset('js/app.js') }}" defer></script>, then the x-mask plugin will not working.

If I remove defer and use <script src="{{ asset('js/app.js') }}" ></script>, the x-mask works, but there is a warning Alpine Warning: Unable to initialize. Trying to load Alpine before is available. Did you forget to adddeferin Alpine's tag? on the chrome browser console.

What should I do? Any suggestion? Thank you!

Advertisement

Answer

Thanks to @Dauros, it works after I put an x-data directive to a parent element. (I also put the defer to the app.js)

JavaScript
JavaScript

It doesn’t mention on the alpineJS x-mask plugin page.

And I finally understand what the document said about the x-data in the Start Here and x-data.

Everything in Alpine starts with the x-data directive.

Everything in Alpine starts with an x-data directive. Inside of x-data, in plain JavaScript, you declare an object of data that Alpine will track.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement