我正在使用vuejs 2.0创建一个webapp 。我使用以下代码创建了简单的选择输入:
<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>
我在data
Vue组件中有这个:
data () {
return {
age: "",
}
},
watch: {
age: function (newAge) {
console.log("log here")
}
但是,当为select添加默认值时,我开始出现此错误:
./~/vue-loader/lib/template-compiler.js?id=data-v-5cf0d7e0!./~/vue-loader/lib/selector.js?type=template&index=0!./src/ components / cde.vue模板语法错误:使用v模型时,将忽略内联所选属性on。而是在组件的数据选项中声明初始值。
@ ./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
@多应用
我也尝试在组件的数据部分提供默认值,但是随后什么也没有发生。我v-bind
也尝试过,但随后观察者停止研究年龄变量。
要做的唯一的事情就是selected
从默认值中删除option
:
<select v-model="age">
<option value="" disabled hidden>Select Age</option>
.....
</select>
是的,如果要预先选择某项,则必须设置像这样的值
this.age="1"
,这将导致在呈现DOM时自动选择“ 1 Year”。