VUE2.0学习记录——组件参数校验与非Props特性

橙三吉。 提交于 2019-11-27 05:00:54
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件参数校验与非Props特性</title>
</head>
<body>
<div id="app">
    <child content="helloworld"></child>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    Vue.component('child',{
        props: {
            // content: String, //组件参数类型校验
            // content: Number, //组件参数类型校验
            // content: [Number,String], //组件参数类型校验
            // content: {
            //     type: String,
            //     required: true
            // },
            // content: {
            //     type: String,
            //     required: false,
            //     default: 'defaultValue'
            // },
            content: {
                type: String,
                validator: function (value) {
                    return value.length < 5;
                }
            }
        },
        template: '<div>{{content}}</div>'
    });
    var app = new Vue({
        el: '#app'
    });
</script>
</html>

 

非props特性:非props特性将展示在子组件的dom属性里

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件参数校验与非Props特性</title>
</head>
<body>
<div id="app">
    <child content="haha"></child>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    Vue.component('child',{
        template: '<div>hello world</div>'
    });
    var app = new Vue({
        el: '#app'
    });
</script>
</html>

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