[Vue] props校验以及自定义validator

Validation

例子

  • 参考代码:code
  • 实现功能:
poc各种props的validation的用法

使用思路

  • 限制String类型
传值:
<valid-two :content="2"></valid-two>

使用:
var validTwo = {
	props: {
		content : String
	},
	template: '<div>{{content}}</div>'
}

提示:
[Vue warn]: Invalid prop: type check failed for prop "content". Expected String with value "2", got Number with value 2.
  • 限制Number类型
传值:
<valid-three content="Hello validation three"></valid-three>

使用:
var validThree = {
	props: {
		content : Number
	},
	template: '<div>{{content}}</div>'
}

提示:
[Vue warn]: Invalid prop: type check failed for prop "content". Expected Number with value NaN, got String with value "Hello validation three"
  • 同时限制String,Number类型
传值:
<valid-four :content="{'object':'1'}"></valid-four>

使用:
var validFour = {
	props: {
		content : [String, Number]
	},
	template: '<div>{{content}}</div>'
}

提示:
[Vue warn]: Invalid prop: type check failed for prop "content". Expected String, Number, got Object.

  • 自定义属性
传值:
<valid-five></valid-five>

使用:
var validFive = {
	props:{
		content : {
			type : String,
			required : false,//是否必须
			default : 'Hello validation five',//默认值
			validator: function(value){//自定义validator
				return (value.length < 5)
			}
		}
	},
	template: '<div>{{content}}</div>'
}

提示:
[Vue warn]: Invalid prop: custom validator check failed for prop "content".
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值