能够利用v-model
在表单控件元素上建立双向的数据绑定,v-model
会根据控件类型自动选取正确的方法来更新元素。html
文本框的绑定例子以下:数组
<div id="app"> <p>input</p> <input v-model="message"> <p>{{message}}</p> <p>textarea</p> <textarea v-model="message2"></textarea> </div>
new Vue({ el:'#app', data:{ message:'', message2:'' } })
data
中的值为<input>
的value
,如:app
<div id="app"> <input type="radio" value="Value1" v-model="picked"> <input type="radio" value="Value2" v-model="picked"> <span>选中的值为:{{picked}}</span> </div>
new Vue({ el: '#app', data: { picked:'Value1' } })
单个多选绑定的数据是一个布尔值,多个多选绑定的是一个数组:函数
<div id="app"> <input type="checkbox" v-model="checked"> <span>是否选中:{{checked ? "选中" : "不选中"}}</span> <br> <input type="checkbox" value="Value1" id="box1" v-model="checked2"> <label for="box1">Value1</label> <input type="checkbox" value="Value2" id="box2" v-model="checked2"> <label for="box2">Value2</label> <input type="checkbox" value="Value3" id="box3" v-model="checked2"> <label for="box3">Value3</label> <br> <span>选中的值为:{{checked2}}</span> </div>
new Vue({ el: '#app', data: { checked:'Value1', checked2:[] } })
<div id="app"> <select name="fruit" v-model="selected"> <option value="">请选择一个</option> <option value="苹果">苹果</option> <option value="香蕉">香蕉</option> </select> <div> 选择的水果是:{{selected}} </div> </div>
new Vue({ el: '#app', data: { selected:'' } })
.lazy
:默认状况下,v-model
在input事件中同步输入框的值与数据,但能够添加一个修饰符.lazy
,从而转变为在change
事件中同步数据,好比<input v-model.lazy="meesage">
.number
:自动将用户的输入值转化为Number
类型,若是原值的转换结果是NaN
会返回原值,好比<input v-model.number="age" type="number">
.trim
:自动过滤用户输入的首尾空格,好比<input v-model.trim="message">
修饰符能够混合使用,例子:ui
<div id="app"> <input type="text" v-model.lazy="test"> <br> <p>{{test}}</p> <input type="text" v-model.number.lazy="test2"> <br> <p>{{test2}}</p> <input type="text" v-model.trim="test3"> <br> <p>{{test3}}</p> </div>
组件能够扩展HTML元素,封装可重用的代码,组件系统能够用独立可复用的小组件来构建大型应用,几乎任意类型的应用界面均可以抽象为一个组件树。
注册一个全局组件语法以下:this
Vue.component(tagName,options)
其中tagName
为组件名,options
为配置选项。注册后,按以下方式调用组件:spa
<tagName></tagName>
全局组件就是全部实例都能使用的组件,例如:3d
<div id="app"> <test-title></test-title> </div>
Vue.component('test-title',{ template:'<h1>Test Title</h1>' }) new Vue({el:'#app'})
注意标签名不能大写,好比写成:code
<div id="app"> <testTitle></testTitle> </div>
Vue.component('testTitle',{ template:'<h1>Test Title</h1>' })
页面不会显示标题。component
局部组件就是在本实例内使用的组件,好比:
<div id="app"> <test-title></test-title> </div>
new Vue({ el: '#app', components: { 'test-title': { template:'<h1>Test Title</h1>' } } })
也能够把模板的内容分离出来成为一个变量:
var myTemplate = { template:'<h1>Test Title</h1>' } new Vue({ el: '#app', components: { 'test-title': myTemplate } })
prop
prop
是子组件来接受父组件传递过来的数据的一个自定义属性,父组件的数据须要经过props
把数据传递给子组件,子组件须要显示地使用props
选项声明prop
:
<div id="app"> <test-title title="Test Title"></test-title> </div>
Vue.component('test-title',{ props:['title'], template:'<h1>{{title}}</h1>' //template:'<h1>{{this.title}}</h1>' }) new Vue({el: '#app'})
prop
相似于v-bind
绑定HTML特性到一个表达式,也能够利用v-bind
动态绑定props
值到父组件的数据中,每当父组件的数据变化时,该变化会传递给子组件:
<div id="app"> <input v-model="message"> <br> <test-title v-bind:title="message"></test-title> </div>
Vue.component('test-title',{ props:['title'], template:'<h1>{{title}}</h1>' }) new Vue({ el: '#app', data: { message:'' } })
首先当输入框内容发生变化时,更新父组件的message
,再传递给子组件的title
,最后更新<test-title>
的内容。
下面是一个绑定无序列表的例子:
<div id="app"> <ol> <test-item v-for="i in items" v-bind:val="i"></test-item> </ol> </div>
Vue.component('test-item',{ props:['val'], template:'<h1>{{val.text}}</h1>' }) var vm = new Vue({ el: '#app', data: { items:[ {text:'111'}, {text:'222'} ] } })
注意prop
是单向绑定的,当父组件属性变化时传导到子组件,可是不会反过来。
父组件使用props
传递数据给子组件,若是子组件把数据传递回去须要使用自定义事件,能够在v-on
绑定自定义事件,每一个Vue实例都实现了事件接口,也就是:
$on(eventName)
监听事件$emit(eventName)
触发事件另外父组件能够在使用子组件的地方直接用v-on
来监听子组件触发的事件,例子:
<div id="app"> <p>总计:{{total}}</p> <test v-on:increment="incrementTotal"></test> <br><br> <test v-on:increment="incrementTotal"></test> </div>
Vue.component('test',{ template:'<button v-on:click="incrementHandler">点击增长,目前为{{counter}}</p>', data:function(){ return { counter:0 } }, methods:{ incrementHandler:function(){ this.counter += 1 this.$emit('increment') } } }) new Vue({ el: '#app', data: { total:0 }, methods:{ incrementTotal:function(){ this.total += 1 } } })
效果:
当点击任意一个按钮增长时,更新子组件内的counter
,同时使用this.$emit
向父组件传值,这里没有参数,若是有参数的话在后面加上便可:this.$emit("func",parm)
。
父组件中引用子组件的地方须要添加v-on:func
,其中v-on:func
中的func
须要与this.$emit("func")
中的func
同名,接着在v-on:func="func2"
中修改func2
为父组件的函数便可。简写方式为:
@func="func2"
在某个组件的根元素上监听一个原生事件可使用.native
修饰v-on
,好比:
<test-title v-on:click.native="func"></test-title>
data
上面的例子中data
不是一个对象而是一个函数,若是data
直接返回一个已有对象会影响其余实例,好比修改上面的data
为:
var counter = { counter:0 } //... data:function(){ return counter }
效果以下:
也就是子组件共享了数据,而修改成:
data:function(){ return { counter:0 } }
效果以下:这是由于返回给每一个实例一份独立的拷贝。