I'm trying to implement such algo using vueJS:
:value.sync="firstValue" and :value.sync="secondValue";v-model="value";@keyup="$emit('update:value', value);";Again, algorithm performing well. But I think I'm doing bad with such things. I mutate prop value in each component using v-model, which isn't good. But .sync modifier won't work on value - I've checked, it only works on props, because v-bind only works on props as I can see... So, I'm very glad to hear advises for improvement..
You could consider using a store like Vuex in this case.
You can use v-model with components. Docs
That said, I would do something like this: Codepen
<field v-model="text"></field>
<field v-model="processedText"></field>
and then use watchers
watch: {
text(text) {
this.processedText = this.process(text);
},
processedText(text) {
this.text = this.process(text);
}
}
Of course there's probably more than one way to do it, but this seems like the shortest route :)
Thanks to all answers you guys provided. I've learned some new tricks from these advises. But I'd like to make a clarification and provide the best(for my opinion) choice!
For the start, my method:
@input="$emit('input', $event.target.value)" v-bind:value="value", where value is a defined prop;@input="someValueChanged">. And we could even have a two-way listener field inside parent, by just using v-model on our custom components: v-model="some value".The pros of my methods are clear: we have universal component for ANY input type. We can make a custom listeners by listening directly to emitted events. And we can put a needed modifiers, like .lazy on those custom inputs. There are no disadvanatages tbh, or I haven't found them yet.
vch's answer: Cool method, I've tried it, but it seems to be badly recursive inside parents component. Your codepen works, but in my case(VueJS2) it seems to be broken.. But I've started from your method and just removed useless watchers and added event listeners.
Pavan's answer: Again, working method. We could really use storage for such case, but I see sort of disadvantages here:
Thx you all! It was very useful!