Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
VueJS sync two input fields

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

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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
about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

0

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!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda