VueJS Custom Props Validation Function

此生再无相见时 提交于 2019-12-05 17:08:47

You might want to checkout vue-properties:

import {biggerThan} from 'vue-properties';

export default {
    props: {
        canDrink: {
            type: Integer, 
            validator: biggerThan(18)
        },
    }
}

According to the docs:

It is possible for a component to specify requirements for the props it is receiving. If a requirement is not met, Vue will emit warnings. This is especially useful when you are authoring a component that is intended to be used by others.

What they're saying here is that the validators you specify, should be met at all times for this property to properly work. If not, they'll emit a warning, just like the one you're experiencing.

When you define the validator like this, you're saying that all input for this specific validator should be larger than 10.

        validator: function (value) {
            console.log("inside validator: " + value);
            return value > 10;
        }

Then when you bind the number 5 as value to this specific property, the validator returns false and emits the warning.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!