简体   繁体   English

如果不通过验证器,Vue props 验证器如何设置默认值

[英]Vue props validator how to set a default value if dont pass validator

Is there a way to set a default value to a prop with validator if the current prop dont pass the validator, i want to set the value to 1 if it is less than 3, here a exemple.如果当前的道具没有通过验证器,有没有办法为带有验证器的道具设置默认值,如果它小于 3,我想将值设置为 1,这里是一个例子。

currentStep: {
      type      : Number,
      required  : true,
      validator : (value) => {
        return value <= 3;
      },
      default   : 1
} 

The validator method always returns a boolean value, it's not used to return any other value, you could use a computed property based on the prop and validate it : validator方法总是返回一个布尔值,它不用于返回任何其他值,您可以使用基于 prop 的计算属性并对其进行验证:

props:{
   currentStep: {
      type      : Number,
      required  : true,
      default   : 1
 },
},
computed:{
  step(){
      return this.currentStep<=3 ? 1 : this.currentStep
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM