32-vue的父传子props属性基本使用(对象形式)可类型限制,验证,默认值

验证支持的类型
String
Number
Boolean
Array
Object
Date
Function
Symbol

1、类型限制(单值)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id ="app">
    <!-- 使用父组件的数据时,必须使用v-bind指令 -->
    <cpn :cmessage="message"></cpn>
  </div>

  <template id="cpn">
    <div>
      <h2>{{cmessage}}</h2>
    </div>
  </template>

  <script>

  // 父传子:props
  const cpn={
    template : '#cpn',
    props:{
      // 1,类型限制( 'null'匹配任何类型)
      cmessage:String
    }
  }

   //创建Vue实例,得到 ViewModel
   var vm = new Vue({
    el: '#app',
    data:{
      message: '好冷啊',
      movides:['温暖的抱抱','拆弹专家','送你一朵红花']
    },
    components:{
      cpn
    }
   });
  </script>
</body>

</html>

2、类型限制(多个限制)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id ="app">
    <!-- 使用父组件的数据时,必须使用v-bind指令 -->
    <cpn :cmessage="message"></cpn>
  </div>

  <template id="cpn">
    <div>
      <h2>{{cmessage}}</h2>
    </div>
  </template>

  <script>

  // 父传子:props
  const cpn={
    template : '#cpn',
    props:{
      // 2,类型限制( 'null'匹配任何类型)
      cmessage:[String,Number]
    }
  }

   //创建Vue实例,得到 ViewModel
   var vm = new Vue({
    el: '#app',
    data:{
      message: '好冷啊',
      movides:['温暖的抱抱','拆弹专家','送你一朵红花']
    },
    components:{
      cpn
    }
   });
  </script>
</body>

</html>

3、required

使用required必须使用v-bind绑定相应的值

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id ="app">
    <!-- 使用父组件的数据时,必须使用v-bind指令 -->
    <cpn></cpn>
  </div>

  <template id="cpn">
    <div>
      <h2>{{cmessage}}</h2>
      <h2>{{cmovides}}</h2>
    </div>
  </template>

  <script>

  // 父传子:props
  const cpn={
    template : '#cpn',
    props:{
      cmessage:{
        type: String,
        required: true
      },
      cmovides:{
      	type: Array
      }
    }
  }

   //创建Vue实例,得到 ViewModel
   var vm = new Vue({
    el: '#app',
    data:{
      message: '好冷啊',
      movides:['温暖的抱抱','拆弹专家','送你一朵红花']
    },
    components:{
      cpn
    }
   });
  </script>
</body>

</html>

在这里插入图片描述

4、带有默认值的字符串

使用默认值,则不需要使用v-bind绑定。如果使用了v-bind绑定,则默认值无效。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id ="app">
    <!-- 使用父组件的数据时,必须使用v-bind指令 -->
    <cpn></cpn>
  </div>

  <template id="cpn">
    <div>
      <h2>{{cmessage}}</h2>
    </div>
  </template>

  <script>

  // 父传子:props
  const cpn={
    template : '#cpn',
    props:{
      cmessage:{
        type: String,
        default : '踏山河'
      }
    }
  }

   //创建Vue实例,得到 ViewModel
   var vm = new Vue({
    el: '#app',
    data:{
      message: '好冷啊',
      movides:['温暖的抱抱','拆弹专家','送你一朵红花']
    },
    components:{
      cpn
    }
   });
  </script>
</body>

</html>

5、带有默认值的数组

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id ="app">
    <!-- 使用父组件的数据时,必须使用v-bind指令 -->
    <cpn></cpn>
  </div>

  <template id="cpn">
    <div>
      <h2>{{cmessage}}</h2>
    </div>
  </template>

  <script>

  // 父传子:props
  const cpn={
    template : '#cpn',
    props:{
      // 对象或数组默认值必须从一个工厂函数获取
      cmessage:{
        type: Array,
        default(){
          return ['真冷','生死无话']
        }
      }
    }
  }

   //创建Vue实例,得到 ViewModel
   var vm = new Vue({
    el: '#app',
    data:{
      message: '好冷啊',
      movides:['温暖的抱抱','拆弹专家','送你一朵红花']
    },
    components:{
      cpn
    }
   });
  </script>
</body>

</html>

6、使用validator

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id ="app">
    <!-- 使用父组件的数据时,必须使用v-bind指令 -->
    <cpn :cmessage="message"></cpn>
  </div>

  <template id="cpn">
    <div>
      <h2>{{cmessage}}</h2>
    </div>
  </template>

  <script>

  // 父传子:props
  const cpn={
    template : '#cpn',
    props:{
      cmessage:{
        // cmessage值必须匹配下列字符串中的一个,否则会报错
        validator(value){
          return ['success','warn'].indexOf(value)!==-1;
        }
      }
    }
  }

   //创建Vue实例,得到 ViewModel
   var vm = new Vue({
    el: '#app',
    data:{
      message: 'warn',
      movides:['温暖的抱抱','拆弹专家','送你一朵红花']
    },
    components:{
      cpn
    }
   });
  </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值