1

In my vue application I've got lots and lots of input fields (example):

<div class="field">
    <label for="name" class="label">Naam</label>

    <div class="control">
        <input id="name"
               name="name"
               type="text"
               v-model="relation.name"
               class="input"
               :class="{ 'is-danger': errorsHas('name') }"
               autofocus>

        <p class="help is-danger" v-if="errorsHas('name')">{{ error('name') }}</p>
    </div>
</div>

So I would like to wrap this in a input component. But since vue 1 the .sync method is gone so how would I do this? Firing events is not realy a solution I guess. Just wondering how to solve this?

I would like to have something like this:

<custom-input v-model=relation.name></custom-input>

And everything else (class name, autofocus etc...) must be handled in that component.

Is this possible?

2 Answers 2

10

Sync modifier was reintroduced in 2.3.0+, see Vue Js Docs.

In 2.3.0+ we re-introduced the .sync modifier for props, but this time it is just syntax sugar that automatically expands into an additional v-on listener:

The following <comp :foo.sync="bar"></comp> is expanded into:

<comp :foo="bar" @update:foo="val => bar = val"></comp>

For the child component to update foo‘s value, it needs to explicitly emit an event instead of mutating the prop:

this.$emit('update:foo', newValue)

You may see this fiddle as an example.

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

7 Comments

Just what I need! But when and where would I fire: this.$emit('update:foo', newValue) ?
@Jenssen In your example what is the property you are trying to update?
I'm trying to update relation.name in a parent component.
@Jenssen You can do <custom-input :relationName.sync="name"></custom-input>, define relationName as a prop in your component, and then on your method where you update this.relationName = newVal just trigger this.$emit('update:relationName', newValue)
@Jenssen I've expanded @enriqg9's fiddle to demonstrate how you can use sync to a component with an input. The component can use a settable computed to handle the event emits, and the input can use v-model with that computed.
|
0

You may use props to be more specific dynamic props.

Just set up a appropriate component which accepts a prop and reference to that prop in your components v-model.

Your component could look like this (adjusted example from official site):

Vue.component('custom-input', {
  // declare the props
  props: ['message'],
  // just like data, the prop can be used inside templates
  // and is also made available in the vm as this.message
  template: '<input v-model="message">'
})

While your parent would use it like that:

<custom-input :message="parentMsg"></custom-input>

1 Comment

thanks. But then I get: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "name"

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.