question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sync Modifier support object properties

See original GitHub issue

What problem does this feature solve?

Sync Modifier support object properties. For example, I have a myObj object, it has a foo and a bar property.

myObj: {
  foo: 'foo',
  bar: 'bar'
}

This feature could use update:myObj.foo and update:myObj.bar to update the value.

this.$emit('update:myObj.foo', 'new-foo');
this.$emit('update:myObj.bar', 'new-bar');

What does the proposed API look like?

Currently, I need to use below format

<MyComponent :foo.sync="myObj.foo" :bar.sync="myObj.bar"></MyComponent>
props: ['foo', 'bar']
...
this.$emit('update:foo', 'new-foo');
this.$emit('update:bar', 'new-bar');

But with this new feature:

<MyComponent :myObj.sync="myObj"></MyComponent>
props: ['myObj']
...
this.$emit('update:myObj.foo', 'new-foo');
this.$emit('update:myObj.bar', 'new-bar');

It becomes more simple and clear, especially when the object has many properties.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
yyx990803commented, Sep 1, 2017

Although I can see the point of this proposal, I don’t feel that it’s particularly needed. In some way it encourages passing multiple values in an object to avoid having to specify multiple props. It is very easy to abuse object props by tucking more and more properties into it, making it some sort of “super prop”. However, I think having an explicit list of props makes your component’s interface easier to understand.

1reaction
jackchouminecommented, Sep 2, 2020

You can use v-bind without directive arguments:

<MyComponent v-bind="myObj"></MyComponent>
...
props: ['foo', 'bar']
...

using sync with an object work well.

<child :my-obj.sync = "myObj"/>

myObj take more than two key. inner child

this.$emit("update:myObj",newObj)

who is more harder to understand? who is better? why?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue's new and improved prop.sync. In Vue 2.3, the ... - Medium
sync modifier, which was removed in the past, has been reintroduced in a new way. This post is going to explore two approaches...
Read more >
Issue with passing objects via sync? - Get Help - Vue Forum
I am new to vue js. I got stuck with a problem with vue js. Here is the fiddle. https://jsfiddle.net/tmwbkL40/ <div class="apple"> <inputt ......
Read more >
Vue.js Emit props & Sync modifier - Francium Tech
External JavaScript​​ sync modifier comes with even more flexibility for updating multiple props in objects. Checkout the docs here. Note of caution: There...
Read more >
Confusion with Object-Props and Sync Modifier : r/vuejs - Reddit
With .sync, the child component has no ability to control the modifications. It's all controlled by the parent. The child just sends an...
Read more >
Vue JS Tutorial - Props, Events, Sync Modifier [ Component ...
Let's look at transmitting data between parent and child components using Props, Custom Events and the . sync Modifier (links below).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found