props

propos是组件的自定义属性

在Count中:

props:['init'](在export default中,与data平级.)//init的名字是自定义的,一般叫init就行。

<MyCount init="8"></MyCount>

{{init}}

但是props里的值是只读的,程序员不能直接修改,否则终端会报错。

<button @click="init++">+1</button>    是错误d

<template>
<div>
  <button id="art">Mycount</button>
  <div>count的值是:{{count}}</div>
  <button @click="count++">+1</button>
  <button @click="show">打印this</button>
</div>
   <!--只有在add只有一行时才能使用-->
</template>

<script>

export default {
   props:['init'],
   data(){
    return{
      count:this.init
    }
  },
  methods:{
    show(){
      console.log(this)}
      
  }
}
</script>

<style>
#art{
  color:blue
}
</style>

props-default默认值

 当在<Mymount>中没有传递初始值

props:{
    //自定义属性A:{/*配置选项*}
    //自定义属性B:{/*配置选项*/ }
    init:{
      default:0
    }
   },

props-type

props:{
    //自定义属性A:{/*配置选项*}
    //自定义属性B:{/*配置选项*/ }
    init:{
      default:5,
      type:Number
      //Number必须大写,还可以是Array,Object,Boolean.
    }
   },

props-required,要求用户必须在实例化的时候声明init

props:{
    //自定义属性A:{/*配置选项*}
    //自定义属性B:{/*配置选项*/ }
    init:{
      default:5,
      type:Number,
      //Number必须大写,还可以是Array,Object,Boolean.
      required:true
      //校验是否填了init项
    }
   },

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值