1.父组件向子组件传递数据可以通过 props
2.子组件向父组件传递数据可以通过自定义事件,父组件自定义事件,子组件触发父组件的事件,并传传递数据
3.子组件可以定义插槽slot,让父组件自定义要显示的内容
4.使用easycom规范,可以真接使用组件
<template>
<view>
<veiw>自定义组件使用规范</veiw>
<card color="red" @fclick="fclick"></card>
<card color="yellow">黄色组件</card>
</view>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
fclick(msg){
console.log('父组件收到子组件传递的值:'+msg);
}
}
}
</script>
<style>
</style>
组件:components/card/card.vue
<template>
<view :style="{background:color}" @click="zclick">
自定义组件<slot></slot>
</view>
</template>
<script>
export default {
name:"card",
props:{
color:{
type:String,
default:'white'
}
},
data() {
return {
};
},
methods:{
zclick(){
console.log('点了子组件');
this.$emit('fclick','定击事件传递给父组件');
}
}
}
</script>
<style>
</style>
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注华域联盟的更多内容!
您可能感兴趣的文章:
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)