Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue property not valid with underscore

I was doing some tests and I noticed that my properties are not valid when I used underscore.

Example:

new Vue({
el : "#form",

data: {
    errors: [],
    _username: '',
    _password: ''
});

on html file:

<input class="uk-input" type="text" v-model="_username" >
<input class="uk-input" type="password" v-model="_password">

With the code above the app won't render. If I remove the underscore it will work, does someone knows why this happens?

like image 882
Disturb Avatar asked Sep 18 '17 02:09

Disturb


People also ask

What does the underscore mean in Vue?

As was stated many professional developers have come to use _ (underscore) as a prefix for special cases (Even the developers of Vue found it useful for their purposes!) It is one of two non alpha characters that can be used to start a variable name, $ being the other and is also take by Vue!

Should always be multi word Vuejs?

User component names should always be multi-word, except for root App components. This prevents conflicts with existing and future HTML elements, since all HTML elements are a single word.


1 Answers

The answer may be found in the documentation

Properties that start with _ or $ will not be proxied on the Vue instance because they may conflict with Vue’s internal properties and API methods. You will have to access them as vm.$data._property

In your templates, you will have to reference $data._username / $data._password, eg

<input class="uk-input" type="text" v-model="$data._username" >
<input class="uk-input" type="password" v-model="$data._password">

Demo here ~ https://jsfiddle.net/9bzxuecj/2/

like image 187
Phil Avatar answered Oct 16 '22 02:10

Phil