The subject of the question was written in vue because there was no requested data, so some fake data was created:
This is a dynamically generated img tag
Then use on the page:
But when I opened chrome and looked at the effect, I found that this cannot be swiped and I can only see the initial photo
The dynamically generated img is also correct. The structure is as follows, but why can’t it be swiped?
And what’s strange is that the questioner changed the dynamically generated img tag to 3’ <p class="swiper-slide">Slide 1</p>
' and it can’t be swiped. I looked at the swiper official website There is no additional swiping method in the document. May I ask what is wrong with this?
The event may not be bound. You can try putting new swiper in the mounted life cycle, or use vue-swiper to encapsulate it by others
The reason is not detailed enough. We can only speculate that it may be in the "dynamic generation" area. It is like if you bind events to elements and follow the conventional method ele.addEventListener to dynamically generated elements, it will fail.
In the code written by the original poster, why is swiper-container used repeatedly? ? ?
I’ll give you what I wrote. You can compare it. Yours is too unclear
html
js:
swiper.update() try
I found the reason. The reason is that the position of initializing swiper is wrong. To dynamically generate img tags, new swiper must be created after all tags are rendered on the interface. I use vue to write components. The original code is like this
The bound data source has been changed to generate components asynchronously, so the operation of initializing the swiper must be executed in the life cycle after the component is loaded, that is, in the mounted function of the Vue object