组件

博客介绍了Vue的全局组件、局部组件,props属性的动态绑定、类型验证,子组件自定义方法等内容。同时提醒,因html不区分大小写,常使用横线连接,Vue内部会将其转为驼峰命名,且html会把大写字母解析为小写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 全局组件
<body>
    <div id="app">
        <child ></child>
    </div>

    <script>
        // 全局组件
        Vue.component('child',{
           template:'<h1>child<h1/>'
        })

        new Vue({
            el:"#app"
        })
    </script>
</body>
  1. 局部组件
<body>
    <div id="app">
        <child ></child>
    </div>

    <script>
        var childComponent = {
            template:"<h1>局部组件<h1/>"
        }
        // 局部组件
        new Vue({
            el:"#app",
            components:{
                'child':childComponent
            }
        })
    </script>
</body>
  1. props属性
<body>
    <div id="app">
        <child message="test"></child>
    </div>

    <script>

        //prop
        Vue.component('child',{
            props:['message'],
            template:'<h1>{{message}}<h1/>'
        })
        new Vue({
            el:"#app"
        })
    </script>
</body>
  1. 动态绑定
<body>
    <div id="app">
        <input type="text" v-model="message">
        <child  v-bind:test="message"></child>
    </div>

    <script>

        //prop动态绑定
        Vue.component('child',{
            props:['test'],
            template:'<h1>{{test}}<h1/>'
        })
        new Vue({
            el:"#app",
            data:{
                message:'test'
            }
        })
    </script>
</body>
<body>
    <div id="app">
        <ol>
            <child v-for="item in message" :test="item"></child>
        </ol>
    </div>

    <script>

        //prop动态绑定
        Vue.component('child',{
            props:['test'],
            template:'<li>{{test.name}}</li>'
        })
        new Vue({
            el:"#app",
            data:{
                message:[
                      {name:'小明'},
                      {name:'小红'},
                      {name:'小李'},
                ]
            }
        })
    </script>
</body>
  1. 类型验证
<body>
    <div id="app">
        <ol>
            <child v-for="item in message" test="item"></child>
        </ol>
    </div>

    <script>

        //prop动态绑定
        Vue.component('child',{
            props:{

                test: {
                    type: Object,
                    required:true
                }
            },
            template:'<li>{{test.name}}</li>'
        })
        new Vue({
            el:"#app",
            data:{
                message:[
                      {name:'小明'},
                      {name:'小红'},
                      {name:'小李'},
                ]
            }
        })
    </script>
</body>
  1. 子组件自定义方法
<body>
<div id="app">
    {{count}}
    <br/>
   <child-component :init-value=count @add-event="addFunction"></child-component>
</div>

<script>

    Vue.component('child-component',{
        props:['initValue'],
        data:function () {
            return{
                countValue:this.initValue
            };
        },
        template:'<button @click="addFunction">{{countValue}}</button>',
        methods:{
            addFunction: function () {
                this.countValue++;
                this.$emit('add-event');
            }
        },

    })

    new Vue({
        el:"#app",
        data:{
            count:"11"
        },
        methods: {
            addFunction:function () {
                this.count++;
            }
        }
    })

</script>
</body>
  1. 注意
    1. html不区分大小写,所以往往中间用横线连接。并且vue内部往往会把相应的横线链接的变成驼峰命名的方式。
    2. html不区分大小写,会把所有大写字母解析成小写字母。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值