源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

vue中component组件的props使用详解

  • 时间:2022-05-28 22:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:vue中component组件的props使用详解
本文介绍了 vue中component组件的props使用详解,分享给大家,具体如下: props使用方法
Vue.component('my-component',{ 
       props:['message'], 
      template:'<div class="tem1">{{message}}</div>' 
    }); 
<my-component message="hello"></my-component>
注意:props 的声明需要放在template的前面 props可以使用实例中的变量赋值 全局组件可以获取用使用prop 的做操作 下面例子为message先先渲染为 "hello!!!" click点击事件  调用zan方法为重新为comdata,message赋值 但是只有comdata显示 不能影响message的值显示
 <div id="app"> 
    <my-component v-bind:message='message'></my-component> 
  </div> 
  </body> 
  <script> 
    Vue.component('my-component',{ 
       props:['message'], 
      template:'<div v-on:click="zan">{{comdata}}<div>{{message}}</div></div>', 
      data:function(){return {comdata:this.message}}, 
      methods:{ 
        zan:function(){ 
          this.comdata=this.message+'vue'; 
      this.message=this.message+'vue'   
        } 
      } 
    }); 
 
    var app=new Vue({ 
      el:'#app', 
      data:{message:'hello!!!'} 
    }) 
 </script> 
[img]http://files.jb51.net/file_images/article/201709/20179490214268.png?2017849226[/img] [b]prop验证[/b] 组件为props提供了验证功能
props:{propName: 
  { 
    typpe:[Number,String,Boolean,Function,Array,Object], 
    default:function(){ 
      return {name:'weng'} 
    }, 
    validator:function(value){ 
      return value.length>3 
    } 
  } 
   
} 
ps:type可以自定义 使用instanceof检测 validator验证需要在开发版本vuejs下在控制台才会有输出 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部