首页 > 编程 > JavaScript > 正文

vue中component组件的props使用详解

2019-11-19 15:34:11
字体:
来源:转载
供稿:网友

本文介绍了 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> 

prop验证

组件为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下在控制台才会有输出

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表