简体   繁体   English

Vue.js 道具验证道具

[英]Vue.js prop validation for props

I have a child component with the following props declaration:我有一个带有以下道具声明的子组件:

   props: {
        count: Number,
        maxNum: Number
   }

This is normally fine, but I also have maxNum mapped to an input field with:这通常很好,但我也将maxNum映射到一个输入字段:

<input type="text" v-model="maxNum" value="maxNum">

So even if the user puts "4" in the input, Vue.js thinks that it's a string, when in reality it's a valid number if parsed right.因此,即使用户在输入中输入“4”,Vue.js 也会认为它是一个字符串,而实际上如果解析正确,它是一个有效数字。

I tried doing this, but it didn't fail properly on the input "apple":我试过这样做,但在输入“apple”时它没有正确失败:

   props: {
        maxNum: {
            validator: function (v) {
                parseInt(v);
                return true;
            }
        }
   }

What's the prescribed way of validating props when a v-model is involved?当涉及 v-model 时,验证 props 的规定方法是什么?

Ah, so Vue.JS actually provides a nice way to do this apparently.啊,所以 Vue.JS 实际上显然提供了一种很好的方法来做到这一点。

<input type="text" v-model.number="maxNum" value="maxNum">

The .number modifier lets the v-bind:value part of the v-model equation to treat the input value as a number. .number修饰符让v-model方程的v-bind:value部分将输入值视为数字。

Reference on v-model.number in Vue.JS guide is here . Vue.JS 指南中关于 v-model.number 的参考在这里

I see you figured out how to solve your issue, but I'll still try to explain what went wrong here.我看到你想出了如何解决你的问题,但我仍然会尝试解释这里出了什么问题。

Issue 1第一期

I'm guessing you expected parseInt to break on inputs without a number in them, but that's not the case.我猜你希望parseInt在没有数字的情况下中断输入,但事实并非如此。 parseInt(null) , parseInt('foo') and parseInt([{ bar: 'Baz' }]) will all work fine, they'll just return NaN . parseInt(null)parseInt('foo')parseInt([{ bar: 'Baz' }])都可以正常工作,它们只会返回NaN Consequently, your validator will always proceed to the second line and return true , thus treating any input as valid.因此,您的验证器将始终进入第二行并返回true ,从而将任何输入视为有效。 To fix it, you would have to run parseInt on your input and check if it returns NaN , so your validator would like like this:要修复它,您必须在输入上运行parseInt并检查它是否返回NaN ,因此您的验证器会像这样:

validator: function (v) {
    return !isNan(parseInt(v));
}

Issue 2第 2 期

You're not really looking for a validator.你并不是真的在寻找验证者。 A validator just checks input values and decides whether they are valid or not, it doesn't change them in any way.验证器只是检查输入值并确定它们是否有效,它不会以任何方式更改它们。 In other words, the corrected validator above wouldn't cast '13' to 13 , it would just prevent input like 'apple' or undefined .换句话说,上面更正的验证器不会将'13'转换为13 ,它只会阻止像'apple'undefined这样的输入。 '13' would get through unchanged and you would have to manually cast it later on. '13'将通过不变,您将不得不稍后手动转换它。

The feature you are looking for was provided as an optional coerce function on each component prop in Vue 1, but it was removed for version 2. If you ever need to do it in a slightly more complicated manner that isn't covered by the solution you linked to, the officially recommended way is to simply use a computed value .您正在寻找的功能是作为 Vue 1 中每个组件道具的可选coerce功能提供的,但在版本 2 中已被删除。如果您需要以稍微复杂的方式执行此操作,则解决方案未涵盖您链接到,官方推荐的方法是简单地使用计算值

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

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