vue中component组件的props使用详解

Vue.component('my-component',{ props:['message'], template:'<div>{{message}}</div>' }); <my-component message="hello"></my-component>

注意:props 的声明需要放在template的前面

props可以使用实例中的变量赋值

全局组件可以获取用使用prop 的做操作

下面例子为message先先渲染为 "hello!!!" click点击事件  调用zan方法为重新为comdata,message赋值

但是只有comdata显示 不能影响message的值显示

<div> <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>

vue中component组件的props使用详解

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wygjyz.html