Consider a vanilla JavaScript function like this:
if (window.devicePixelRatio >= 2) {
document.querySelectorAll('img.retina').forEach(function (e) {
let parts = e.src.split('.');
let ext = parts.pop();
if (parts.join('.').indexOf('@2x') == -1) e.src = parts.join('.') + '@2x.' + ext;
});
}
This function changes the src of an image when a retina display is detected. How can one incorporate this function into an application built with vue.js? Where should the function be placed?
If the function is placed in mounted, it only works on the initial page load and not when navigating through SPA pages using the router:
new Vue({
el: "#app",
router,
render: h => h(App),
mounted() {
retinaFunction();
},
})
The specific details of the retina function are not crucial, the main question is how to effectively utilize custom vanilla JavaScript functions within a vue.js application.