E
E
Evgeny2017-07-19 14:33:48
Vue.js
Evgeny, 2017-07-19 14:33:48

How to properly receive parameters and react to their changes?

Good afternoon!
There is approximately the following structure (simplified)
in child:
watch: {
userid(value) { ... },
},
mounted() { ... }
when parent is initialized, it knocks on the server, receives the user's data and saves it in user, watch child 'a sees the changes and correctly fulfills. All OK.
I switch to another route, then I press back and ... everything is bad
, the child watcher is not executed, because user already exists and nothing changes.
is it possible to somehow call the watcher when initializing child?
Of course, you can make the whole watcher into a separate method and call it already, but it doesn’t look quite right
. How to get the parameters and react to their changes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kulakov, 2017-07-19
Lubyanoy @arswarog

Those. you have a component, when you go back on a new one, do not mount it already?
If mounted is still called, then by default setting userid to null, for example, the watcher should eventually be called IMHO.
The doc also says that you can do this:

watch: {
    // строка с именем метода:
    b: 'someMethod'
}

J
JSmitty, 2017-07-19
@JSmitty

If possible, try not to use watchers at all. To update the interface in a child component, in the vast majority of cases, you can use computed properties calculated from props passed outside. Or consider at least partial vuex integration to link components in a hierarchy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question