1

I'm trying to sync a variable between components using the .sync command in Vue. It works without problems when I put components in the page using the tag (for example: <my-component></my-component>). When I bind it in a <router-view>, it works only one-way. Is it normal?

When I click on the link inside the component, it does not change the value of test in the root Vue object. When I change it in the root object, the component inherits the value correctly.

Click here for an example

2
  • Could you elaborate on "works only one-way"? Besides, if you check in the console, Vue actually warns you to avoid mutating a prop directly. Commented May 19, 2020 at 17:29
  • So can't I edit a property? In components it works. Commented May 19, 2020 at 18:11

1 Answer 1

2

You are trying to update the prop directly, the correcto way to update it is to emit an update. Remember :test.sync is a shorthand to v-bind:test and v-on:update:test

On your Foo component:

editTest: function() {     
  this.$emit('update:test', false)  // instead of this.test = false;        
}

Fiddle: https://jsfiddle.net/hansfelix50/u7k5qpwz/

Sign up to request clarification or add additional context in comments.

2 Comments

I tried, but it does not work: jsfiddle.net/o8r9nzv5/10
You need to write on your Foo component (child). I put a js fiddle on my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.