The vue component is not cached, enter the page to initialize the picture long press event, enter the same component for the second time and cause the long press event to be triggered multiple times?

1. I initialize the long press event

2Router

of the picture in mounted.
Feb.28,2021

have not used mui, but read and write
clipboard.png

this is obviously an event delegate. The custom event longtap is delegated to document.body. If there is no refresh in spa, the delegated event on body will certainly not be destroyed, so it does not enter this component once and initializes an event binding, resulting in more and more

.

this has nothing to do with whether Vue caches components or not. You call the binding function in mounted . mounted is a hook and is designed to trigger each time, so bind multiple times.

If you want to bind to document.body , it is a global event and should be placed in the global entry. If you have to bind to a specific component, you should unbind it when destroy .

Menu