Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

placeholder for select in vuejs 2.0.0

I am creating a webapp using vuejs 2.0. I have created simple select input using following code:

  <select v-model="age">     <option value="" disabled selected hidden>Select Age</option>     <option value="1"> 1 Year</option>     <option value="11"> 11 Year</option>   </select> 

and I have this in data of my Vue component:

data () {   return {     age: "",   } }, watch: {         age: function (newAge) {     console.log("log here")   } 

But I start to get this error when adding default value for select:

ERROR in ./~/vue-loader/lib/template-compiler.js?id=data-v-5cf0d7e0!./~/vue-loader/lib/selector.js?type=template&index=0!./src/components/cde.vue template syntax error : inline selected attributes on will be ignored when using v-model. Declare initial values in the component's data option instead.

@ ./src/components/cde.vue 10:23-151

@ ./~/babel-loader!./~/vue-loader/lib/selector.js? type=script&index=0!./src/views/abcView.vue @ ./src/views/abcView.vue

@ ./src/router/index.js

@ ./src/app.js

@ ./src/client-entry.js

@ multi app

I tried to give default value in the data section of the component as well, but then nothing happened. I tried v-bind also but then watchers stopped working on age variable.

like image 490
Saurabh Avatar asked Nov 02 '16 08:11

Saurabh


People also ask

How do you get selected value of select in Vue?

Get Selected Value of Select Dropdown in VueCreated a select box inside the template syntax. Added an onChange() event handler. Created an options list, cars name primarily. Used the on-change method to grab the selected value using the event object.

What does $t mean in Vue?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue. i18n.

What is $V in Vue?

$v is an object that calls vuelidate (at the time of writing the comment, supported in version Vue. js 2.0), which is intended to check every input, which is made in a non-html form.


1 Answers

For others who may be landing on this question, there was an additional step for me to get the default option to appear. In my case, the v-model I was binding to was returning null and rather than an empty string. This meant that the default option was never selected once Vue bindings kicked in.

To solve for this, simple bind the value property of your default option to null:

<select v-model="age">   <option :value="null" disabled>Select Age</option>   ... </select> 

http://jsfiddle.net/2Logme0m/1/

like image 103
Albert Martin Avatar answered Sep 28 '22 19:09

Albert Martin