组件参数的校验和非Prop特性(Vue)

<body>
    <div id="root">
        <child content="hello world"></child>
    </div>
    <script>
        Vue.component("child",{
            props: {
                content: {
                    type: String,//字符的类型
                    required: true,//是否为必填
                    default: 'default Value',//默认值
                    //自定义验证函数
                    validator: function(value){
                        return (value.length > 5)
                    }
                }
            },
            template: "<div>{{content}}</div>"
        })
        var vm = new Vue({
            el: "#root",
            data: {

            }
        })
    </script>
</body>

总结:

props特性:要求父组件要传,子组件要接,然后可以在组件里直接用父组件传过来的数据,同时,props特性不会显示在元素的Dom标签当中

非Props特性:父组件要传,子组件不接,子组件没法去使用父组件传过来的数据,对应的组值会显示在子组件最外层标签的html的属性之中

猜你喜欢

转载自blog.csdn.net/twinkle_J/article/details/81092125