props属性

1.字符串数组,对象

数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <div id="app">
        <!-- 使用组件称为父组件 -->
      <!-- 绑定msg进行动态改变 -->
      <mycom1  hello="你好" parent-msg="这是来自父组件的msg"></mycom1>
    </div>

    <script>
        //创建组件称为子组件,父组件将数据传递给子组件
        //小驼峰命名,html不能识别大小写,需要将m小写
        Vue.component('mycom1',{
            props:['parentMsg','hello'],
            template:`<div>
                <p>{{hello}}</p>
                <p>{{parentMsg}}</p>
                <p>{{childmsg}}</p>
                </div>`,
            data(){

                return{
                    childmsg:'这是子组件'
                }
            }
        })

    </script>

    <script>
    var vm=new Vue({
        el:'#app',


} );
</script>
</body>
</html>

 

数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <div id="app">
        <!-- 使用组件称为父组件 -->
      <mycom1 :parent-msg="msg"></mycom1>
      <p>父组件的msg值:<input type="text" v-model="msg"></p>
    </div>

    <script>
    var vm=new Vue({
        el:'#app',
        data:{
            msg:''
        },
        method:{},
        components:{
            mycom1:{
                template:`<div>
                <p>{{parentMsg}}</p>
                </div>`,
                props:['parentMsg'],
            }
        }

} );
</script>
</body>
</html>

 

对象

props不可更改

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <div id="app">
        <!-- 使用组件称为父组件 -->
      <mycom1 :parent-msg="msg"></mycom1>
    </div>
    
    <script>
    var vm=new Vue({
        el:'#app',
        data:{
            msg:'父组件的msg'
        },
        components:{
            mycom1:{
                template:`<div>{{parentMsg}}---{{sonfromparent}}</div>`,
                props:['parentMsg'],
                data(){
                    return{
                        sonfromparent:this.parentMsg
                    }
                }
            }
        }

} );
</script>
</body>
</html>

 

style绑定

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <div id="app">
        <!-- 使用组件称为父组件 -->
      <mycom2 :width="msg"></mycom2>
    </div>
    
    <script>
          Vue.component('mycom2',{
            template:`<div :style='childwidth' style='border:1px solid black'>
                     我是子组件mycom2
                </div>`,
                props:['width'],
            computed:{
                childwidth:function(){
                    return{
                        width:this.width+'px'//width:200px
                    }
                }
            
            }
        })

    var vm=new Vue({
        el:'#app',
        data:{
            msg:'200'
        },

} );
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值