Replies: 2 comments 1 reply
-
|
You are using a non- The best way to prevent loading issues is to leverage ES modules. If you export |
Beta Was this translation helpful? Give feedback.
-
|
Update: I got this working. It turns out there were other JavaScript errors that prevented the entrypoint from loading entirely. My code snippets above work, with the only caveat that I need to wrap my window object function calls in a document ready event, like so: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have defined some functions in my
app/javascript/entrypoints/application.tsto be used in JavaScript in my Rails views (app/views). Here's an example:In my view, I use this function within a script tag, as follows:
On my development environment, when I load the page, I am getting
Uncaught ReferenceError: renderChSwiper is not defined.When the page finishes loading, I also don't see the function defined on the window object when I go into the browser's developer tools console and typewindow.renderChSwiper. In Webpacker, this worked without an issue, and it seems Vite is doing something differently with regards to the window object.I understand this is not best practice, but I am converting a decent-sized application from Webpacker, and this pattern is used a lot.
How can I define functions on the window object?
Beta Was this translation helpful? Give feedback.
All reactions