I added photoswipe (v5) to a component like this:
mounted () {
if (!document.getElementById('psw')) {
const script = document.createElement('script')
script.id = 'psw'
script.type = 'module'
script.innerHTML = `
import PhotoSwipeLightbox from '/js/photoswipe/photoswipe-lightbox.esm.min.js';
const lightbox = new PhotoSwipeLightbox({
gallerySelector: '#gallery--productimages',
childSelector: 'a',
pswpModule: '/js/photoswipe/photoswipe.esm.min.js'
});
lightbox.init();
`
document.head.appendChild(script)
}
},
The photoswipe files are in the static folder. The script tag is added to the DOM, but photoswipe does not execute when the page is loaded the first time (from internal route link and direct server call). When I gob back an open the page again by the internal link, or refresh the page, it works. I only want to use photoswipe in this component. How can I make it work for the first page load also?