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.

v-model with uninitialized property

See original GitHub issue

Vue.js version

2.0 rc

Reproduction Link

http://codepen.io/anon/pen/xEqbmN?editors=1010#0

What is Expected?

Not sure if this is what is expected in Vue 2.0. Tried to search but couldn’t find anything. e.g:

data: {
  model: {
    a: ''
  }
}
<input v-model="model.a" />
<input v-model="model.b" />

If you pass an uninitialized property (model.b) to v-model, it’s obviously not reactive so bindings won’t update or a deep watch on this.model won’t fire off either.

Any chance that v-model could use Vue.set internally if it detects a binding with value undefined to automatically create reactive properties? (like Vue 1.0)

Or failing that, it should warn when an uninitialized property is passed to v-model

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

104reactions
yyx990803commented, Sep 23, 2016

This is an intended change in 2.0 - v-model no longer creates non-existent properties for you, the basic idea is that all root-level properties must be declared in data at instance creation.

59reactions
neitanodcommented, Apr 21, 2017

According Vue’s documentation, v-model="item.prop" is just syntactic sugar for: v-bind:value="item.prop" v-on:input="item.prop = $event.target.value".

The problem is that WHILE IT STILL CREATES THE PROPERTY it does not make it reactive, so when you combine it with a v-for with dynamically added objects you could end up with a collection of objects, some of them reactive and some of them not reactive.

But, as I said, v-model still creates the property, so the desired effect of motivating you to declare things properly is achieved by the silent threat of reactiveness bugs exploding on your user’s faces instead of a proper warning or just the mitigation of a more common sense v-model behavior.

So to get what we all (except Evan) want, we should stop using v-model="item.prop" and start writing: :value="item.prop" @input="$set(item,'prop',$event.target.value)" which is sugar free and sad.

Read more comments on GitHub >

github_iconTop Results From Across the Web

VueJS custom v-model without value property - Stack Overflow
v -model is syntax sugar for => :value="modelValue" @input="modelValue = $event.target.value". And hence your variable is being updated.
Read more >
VueJS - Different ways to implement v-model
Here are some ways of implementing your own custom v-model : Local variable watcher; Custom method; "Powerful" computed property ...
Read more >
V-model support without requiring value prop - Localazy
This method returns true when the value prop was set by the parent, not initialized to empty string by the default property. So...
Read more >
All about the V-Model directive in Vue JS | by Sushmita Tardia
Our “inputValue” is a reactive property and hence should be present either as a data property or a computed property. Using as a...
Read more >
Form Handling with v-model - 30 Days of Vue - Fullstack.io
The v-model directive syntax takes an expression which is the name of the data property that the input is bound to: v-model removes...
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