v-model:
一个组件上的 v-model
默认会利用名为 value
的 prop 和名为 input
的事件,但是像单选框、复选框等类型的输入控件可能会将 value
特性用于不同的目的。model
选项可以用来避免这样的冲突。下面是我基于v-mode实现与表单元素同样功能的组件。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js" ></script> </head> <body> <div id="app"> <test v-model="msg"></test> {{msg}} <button @click="test1">我是父组件</button> </div> <script type="text/javascript"> Vue.component("test",{ model:{ prop:"testModel", event:"test-model" }, props:{ testModel:{ type:String, value:"我是子组件的值" } }, methods:{ test:function(){ this.h++ this.$emit('test-model',"我是子组件的" + this.index++); } }, data:function(){ return { index:1 } }, template:'<button @click="test">我是子组件</button>' }); var vm = new Vue({ el:"#app", data:function(){ return { msg:"我是父组件", index:1 } }, methods:{ test1:function(){ this.msg="父组件更改值" + this.index++ } } }); </script> </body> </html> |