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.

Initial inline input value

See original GitHub issue

Vue.js version

2.0.2

Reproduction Link

https://jsfiddle.net/uojhhjr7/ https://jsfiddle.net/k8bfrxwz/

While switching to Vue2, I realize that it’s not possible to define a model initial value from the template anymore.

Let’s say I have a server-side validated form (with a preview powered by Vue) which brings back the user if the validation detected an error.

I’d like to populate the form with the previously typed values (and get the corresponding preview), but I cannot simply set an inline value anymore because Vue warns me with inline value attributes will be ignored when using v-model

I took a look at the migration guide and found a confirmation it was not possible anymore, but I can’t find any workaround.

Of course, the simple solution would be to modify the initial value of the model within the Vue instantiation, but it’s a children component from a vue file, webpacked with other scripts.

I got this solution working, but it seems a little bit overkill to me, regarding the usual elegance of Vue.js.

So here is the aim of this issue : Is there a way to pass an initial value to a v-model from template to Vue as we used to ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
georgecocacommented, Jun 29, 2017

Another simple solution would be to use ref and data attributes to reference the element and then access it once the template has been rendered.

Template <input type="text" ref="name" data-value="John">

Javascript

data() {
    return {
        name: ''
    }
},
mounted() {
    this.name = this.$refs.name.dataset.value; // See note below
}

Note: check for dataset compatibility, otherwise use jQuery or any library to extract data-.

Hope that helps 😉

22reactions
yyx990803commented, Oct 12, 2016

In Vue 2.0, the template is like a function: it gets called every time something changes. With this in mind, having an inline value is basically saying the input’s value is static and should never change - which doesn’t make sense when you are using v-model with it.

This does makes your use case a bit more complicated, but here’s how I’d do it - render your form state in your server HTML output and initialize your component with that data: https://jsfiddle.net/vzns7us7/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get input value into a <form> inline - javascript - Stack Overflow
If you want value from input without using selector, then you can use some thing like this, but remember, the value your are...
Read more >
Inline elements - HTML: HyperText Markup Language | MDN
Inline elements are those which only occupy the space bounded by the tags defining the element, instead of breaking the flow of the...
Read more >
Input.Text - Schema Explorer | Adaptive Cards
The initial value for this field. Type: string; Required: No. id. Unique identifier for the value. Used to identify collected ...
Read more >
Default CSS Display Values for Different HTML Elements
With both block and inline elements, you can (if you choose to) redefine their display value to something other than their default.
Read more >
Form - Ant Design
Includes layout, initial values, validation and submit. expand code ... There are three layout for form: horizontal , vertical , inline . expand...
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