Vue: route watch will trigger in not active component

Created on 20 Apr 2017  ·  2Comments  ·  Source: vuejs/vue

Version

2.2.6

Reproduction link

https://jsfiddle.net/50wL7mdz/28620/

Steps to reproduce

I use keep-alive in three components and write watch route in each component.when route changed, the route watch hook will still trigger in not active component .
Can anyone explain is why?

watch: {
'$route' (to, from) {
this.$refs.dataTable.reset();
}
},
The watch hook may be teardown in not active component.
Can anyone help me?

What is expected?

In my demo,when i enter route /foo ,then leave /foo,then enter /bar,want only trigger 'bar in'

What is actually happening?

roter change will triger all route watch in components that ever loaded
In my demo,when i enter route /foo ,then leave /foo,then enter /bar,will trigger both
'foo in' and 'bar in' and 'dup in'

Most helpful comment

Inactive components are simply moved out of the DOM, they are still reactive to data changes. You can set a flag using activated and deactivated hooks to avoid unnecessary watcher callbacks.

All 2 comments

Inactive components are simply moved out of the DOM, they are still reactive to data changes. You can set a flag using activated and deactivated hooks to avoid unnecessary watcher callbacks.

@yyx990803
I think you mean i should do this

router.beforeEach((to, from, next)=>{
    to.activated=true;
    from.activated=false;
  next();
})

But how i can get its own $route object in component ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guan6 picture guan6  ·  3Comments

hiendv picture hiendv  ·  3Comments

robertleeplummerjr picture robertleeplummerjr  ·  3Comments

wufeng87 picture wufeng87  ·  3Comments

paceband picture paceband  ·  3Comments