Vue表单操作

 
v-model	//双向数据绑定
@click='handle' //点击事件绑定
@click.prevent='handle'	//取消默认行为

表单域修饰符

  • number:转换为数值

  • trim:去掉开始和结尾的空格

  • lazy:将input事件切换为change事件

    <input v-model.number="age" type="number">
    

自定义指令

//注册一个全局自定义指令'v-focus'
Vue.directive('focus',{
//当被绑定的元素插入到Dom中时
inserted:function(el){
//聚焦元素
el.focus()
}
})

//局部指令 'v-color'
directive:{
                color:{
                    bind:function(el,binding){
                        el.style.backgroundColor = binding.value.color;
                    }
                }
            }

计算属性

computed:{
                方法名:function(){
                    return 要计算的公式
                }
            }
//使用方式

计算属性与方法的区别

  • 计算属性是基于它们的依赖进行缓存的
  • 方法不存在缓存

监听器

监听属性的变化,数据一旦发生变化就通知监听器所绑定的方法
使用方法
watch:{
	监听属性:function{val}
}

过滤器

目的:格式化数据
//自定义过滤器
Vue.filter('过滤器名称',function(value){
	//过滤器业务逻辑
})
//过滤器的使用
<div></div>
<div v-bind:id='id|formatId'>

//局部过滤器
filters:{
	过滤器名称:function(){
	return 
	}
}

生命周期

  • 挂载(初始化相关属性)

    • beforeCreate

    • created

    • beroreMount

    • mounted

  • 更新(元素或组件的变更)

    • beforeUpdate
    • updated
  • 销毁(销毁相关属性)

    • beforeDestroy
    • destroyed

修改响应式数据

  • Vue.set(vm.items,indexOfItem,newValue)

  • vm.$set(vm.items,indexOfItem,newValue)

    参数1表示要处理的数组名称
    参数2表示要处理的数组的索引
    参数3表示要处理的的数组的值