[BUG] vue switches routes. Functions in watch are triggered repeatedly.

rt;

now there are two pages An and B, in which page A has a watch listening, and page b is empty. When I switch routes, first switch from page A to page B, and then switch back to page A, then the function in watch will be triggered twice. Continue the switch in the previous step, and the number of triggers will continue to be superimposed. The key point is that I have to switch back to page A.

if I create a new C page, the switching between B page and C page will not cause the function in watch to trigger superposition. It will only be superimposed once when I switch back to page A, and there is no limit to the number of overlays.

I hope the boss will analyze the reason.

PS:vue version: 2.5.2

< hr >

initially suspected to be related to vue-router

May.06,2021

first of all, your route should be wrapped with < kepp-alive > , so that the routing component will not be destroyed when switching. It is easy to understand this and solve the problem.

confirm what is listening in the watch option on the A page, and then check whether the B page has changed the content.

if you want to keep < keep-alive > and do not want A component watch to be executed on other pages, you can make a judgment on the routing attribute $route in the function:

    watch: {
        something(newValue, oldValue) {
            if (this.$route.fullPath === 'A') {
                // do something
            }
        }
    }

watch belongs to vue api has nothing to do with vue-router. For more information, please see https://cn.vuejs.org/v2/api/-sharp.
listens for route changes, the route has two actions, to,from. That's why you triggered it twice. Watch is written on page a, and other page switches will not be triggered, of course. no, no, no.


is keep-alive? used?

Menu