1

I'm trying to implement such algo using vueJS:

  1. We have two input fields, for example, we translate from one language to another. These two fields belongs to the two components that are made for easier integration;
  2. These two components are the same type and are in the one parent "host" component;
  3. Whenever we type something inside one component, it immediately being caught by the parent component and then translates using some algo to value for another one component. Then parent should put that value to the second child's field;
  4. Communication should be in two ways in both child components with their parent. I'm done with this algo, but I think I'm doing wrong.. I'm receiving warning that I mutate prop which is a bad idea. If you know the best way of doing such algo, PLS, advice it. Here is my implementation:
  5. Parent has two values: for the first input and for the second. They are bound to children' props using .sync modifier. Like this: :value.sync="firstValue" and :value.sync="secondValue";
  6. Inside children we have prop named value(on which we sync our parent's data). Then we simply bind this prop using v-model: v-model="value";
  7. And after all raise event for .sync updates: @keyup="$emit('update:value', value);";
  8. On the parent bind some functions for events handling, that doesn't matter.

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..

3 Answers 3

2

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:

  1. In the child component: Note, that I'm using VueJs2, so the configuration of CHILD might be different to use v-model. For inputs inside children we set: @input="$emit('input', $event.target.value)" v-bind:value="value", where value is a defined prop;
  2. In the parent component: We don't need to set watchers. The problem of this decision(which is provided in vch's answer) is that it might be the root of infinity recursion inside parent's component. So we could just listen for input events inside children components, like this: @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:

  1. We use not an out-of-box extension - VueX, which have to be installed. I'm fan of built-in methods tbh;
  2. We can't provide separated logic for every input - we'll have to make custom component for each mechanism, cuz they have to listen to different storage variables.

Thx you all! It was very useful!

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

Comments

1

You could consider using a store like Vuex in this case.

  1. After parent component did the transformation, dispatch action and mutate global store property.
  2. In child 2 component, watch to the changes to this store property and then update it's input model

2 Comments

But using such extension only for that purpose(I don't think I'll reuse it lately..) is not the best idea. I'd like to make such thing using built-in methods tbh. Is it the only way?
@ftelnov VueX is the best way to host shared values in two same-level components. It is an addon library suggested by Vue.js and is very easy to learn and use. Vue supports only father->son component value passing (using props).
1

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 :)

2 Comments

Hey! Trying to implement such algo u provided. I'm getting some kind of recursive problem.. I'm running on vuejs2 while your codepen is written on vuejs3, am I right? May it be the problem? I haven't tried watchers yet and so I have a problem: if I use only one-side watcher, I'm doing well, but when they are both, I'm getting recurrent algorithm... How to avoid it?
Well, it's hard to judge based on text description, maybe the problem is in your second watcher's logic?

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.