Clinton Green
Clinton Green

Reputation: 9977

Change data in component from Vue root method

I need to change a value in a component via a method in my Vue root.

My structure looks like this:

Root
  PostAnswer
    data:
      valueToChange: 'Blah blah blah'

My method is working fine but I cannot get this right.

I've read up on using $emit but I can't get it to work in this instance. Basically I need something like:

$root.PostAnswer.data.valueToChange = null;

Upvotes: 0

Views: 916

Answers (1)

flamisz
flamisz

Reputation: 111

You should definitely use event.

In the root just emit an event and in the component just catch it.

For example: In the route:

window.Event = new Vue();

Than you emit (still in the root) when you want to reset the value in the component:

Event.$emit('reset');

In the component:

mounted() {
  Event.$on('reset', () => {
    valueToChange = null;
  });
}

Upvotes: 3

Related Questions