我是靠谱客的博主 喜悦飞机,这篇文章主要介绍uniapp怎么使用组件,现在分享给大家,希望可以做个参考。

本教程操作环境:windows7系统、uni-app2.5.1版本,该方法适用于所有品牌电脑。

推荐(免费):uni-app开发教程

uniapp使用组件的方法:

1、props(props用于父组件给子组件传递参数,参数可以限制类型,可以设置默认值)

2、$emit(事件名,参数1,参数n):用于向父组件传递事件,可携带子组件的参数

3、ref用于获取某个dom节点或子组件的注册引用信息,在父组件的$refs对象里,分别指向dom元素或子组件的实例

4、如需在基本组件的内置事件传递新的参数,可使用$event占位默认参数,如 @change($event,新的参数)

5、插槽<slot></slot>,里面可填充任何模板

如下代码为一个弹窗组件:

复制代码 {{item.name}} 确定
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<template> <view> <!-- 弹窗 --> <view v-if="showPop"> <view :class="{ani:hasAni}"> <!-- 关闭 --> <view @click="closePop"> <image src="/static/image/icon/close.png" mode=""></image> </view> <!-- 标题 --> <view>{{title}}</view> <textarea :maxlength="max" v-model="textArea" auto-height="true" :placeholder="holder" /> <view v-for="(item,index) in swArr" :key="index"> {{item.name}}<switch color="#009714" :checked="item.value" @change="changeSw($event,index)"/> </view> <!-- 确定按钮 --> <view @click="confirmSet">确定</view> </view> </view> </view> </template> <script> export default { name:"popWindow", props:{ title:{ type:String, default:"标题" }, max:{ type:[Number,String], default:200 }, showPop:{ type:Boolean, default:false }, hasAni:{ type:Boolean, default:true }, holder:{ type:String, default:"请输入..." }, swArr:{ type:Array, default:function(){ return [{name:"开关",value:false}]; } } }, data(){ return { textArea:"" } }, methods:{ closePop(){ this.$emit("close"); }, changeSw(e,i){ //console.log(e); //console.log(i); this.$emit("change",e.detail.value,i); }, confirmSet(){ let _self = this; _self.$emit("click",_self.textArea); } } } </script> <style> .popup_box{ width: 100%; height: 100%; background: rgba(0,0,0,0.5); position: fixed; top:0; left: 0; z-index: 2000; padding:0; .pop_panel{ width: 520upx; height: auto; background: #fff; border-radius: 8upx; position: absolute; top:50%; left: 50%; transform: translate(-50%,-50%); .pop_tit{ width: 100%; padding:30upx 0 10upx 0; font-size: 30upx; text-align: center; font-weight: bold; box-sizing: border-box; } .pop_switch{ width: 100%; box-sizing: border-box; padding:0 30upx; font-size: 28upx; switch{ transform: scale(0.6); } } .pop_confirm{ margin-top:20upx; width: 100%; text-align: center; font-size: 28upx; color: #fff; background: #009714; height: 60upx; line-height: 60upx; border-bottom-left-radius: 8upx; border-bottom-right-radius: 8upx; } .pop_area{ width: 460upx; height: 200upx; min-height: 200upx; padding:20upx 20upx; font-size: 26upx; text-align: justify; box-sizing: border-box; border:2upx solid #e6e6e6; margin:10upx auto; } .pop_close{ width:26upx; height:26upx; position: absolute; right: 2upx; top:-40upx; image{ width: 100%; height: 100%; display: block; } } } .pop_panel.ani{ animation: fadeIn 0.6s ease 0s 1 alternate; animation-fill-mode: backwards; } } </style>
登录后复制

用法:

main.js中注册全局组件使用:

1
2
import popWindow from 'components/uni-part/pop-window.vue' Vue.component('popWindow',popWindow);
登录后复制

页面中调用:

1
<popWindow :showPop="showPop" title="审核意见" holder="请输入您的审核意见" @close="changePop" @click="confirmFun" :swArr="arr" @change="changeSw"></popWindow>
登录后复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
data() { return { showPop:false, arr:[{name:"资质证书",value:true}] } }      methods: { changeSw(e,i){ console.log(e,i); var newArr = _self.arr; newArr[i].value = e; _self.arr = newArr; }, confirmFun(e){ //文本输入框和开关值都在这里了 console.log(e); console.log(_self.arr); _self.changePop(); }, changePop(){ _self.showPop = !_self.showPop; } }
登录后复制

效果如下:

56388a6604a439f50a91732bbf6a300.png

以上就是uniapp怎么使用组件的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是喜悦飞机最近收集整理的关于uniapp怎么使用组件的全部内容,更多相关uniapp怎么使用组件内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部