D
D
dev2_betatransfer2021-10-28 15:13:14
Vue.js
dev2_betatransfer, 2021-10-28 15:13:14

How to not execute parent hook in child component in Vue?

The created or mounted hook works on both the parent and the child component. How can I make sure that the parent hook is not executed in the child component?

const parent = new Vue({
mounted: () => console.log('handler'),
});
const child =  parent.extend({});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2021-10-28
@dev2_betatransfer

This is logical behavior. In the same way, when you inherit a class, you call the parent's constructor, and the parent's hooks work on the child.
Most likely you are doing something wrong and using inheritance for other purposes. Describe the task and we will help you to do it humanly. You can set up custom merge strategies
in Vue , but this is an advanced tool for exceptional cases. This is not something that should be used without thinking carefully.

D
Dmitry Gololobov, 2021-10-28
@dGololobov

Add your hook to an object in extend ?

const parent = new Vue({
mounted: () => console.log('handler'),
});
const child =  parent.extend({
  mounted: () => {}
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question