我是靠谱客的博主 糟糕口红,这篇文章主要介绍浅谈VUE uni-app 自定义组件,现在分享给大家,希望可以做个参考。

1.父组件向子组件传递数据可以通过 props

2.子组件向父组件传递数据可以通过自定义事件,父组件自定义事件,子组件触发父组件的事件,并传传递数据

3.子组件可以定义插槽slot,让父组件自定义要显示的内容

4.使用easycom规范,可以真接使用组件

page/news/news.vue

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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>

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注靠谱客的更多内容!

最后

以上就是糟糕口红最近收集整理的关于浅谈VUE uni-app 自定义组件的全部内容,更多相关浅谈VUE内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部