Home > Article > Web Front-end > How to use components in uniapp
Uniapp uses components: 1. Use props for the parent component to pass parameters to the child component; 2. Use [$emit] to pass events to the parent component, which can carry the parameters of the child component; 3. Use ref to obtain the registration reference information of a certain DOM node or subcomponent.
The operating environment of this tutorial: windows7 system, uni-app2.5.1 version. This method is suitable for all brands of computers.
Recommended (free): uni-app development tutorial
How to use components in uniapp:
1, props
(props are used by parent components to pass parameters to child components. Parameters can limit the type and set default values)
2, $emit
(event name, parameter 1, parameter n): used to pass events to the parent component, and can carry the parameters of the sub-component
3, ref is used to obtain the parameters of a certain DOM node or sub-component Register reference information, in the $refs
object of the parent component, pointing to the instance of the DOM element or sub-component respectively
4. If you need to pass new parameters in the built-in event of the basic component, you can Use $event
to place default parameters, such as @change($event, new parameter)
5, slot<slot></slot>
, any template can be filled in it
The following code is a pop-up component:
<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>
Usage:
Register the global component in main.js and use:
import popWindow from 'components/uni-part/pop-window.vue' Vue.component('popWindow',popWindow);
Called in the page:
<popWindow :showPop="showPop" title="审核意见" holder="请输入您的审核意见" @close="changePop" @click="confirmFun" :swArr="arr" @change="changeSw"></popWindow>
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; } }
The effect is as follows:
##Related free learning recommendations:
The above is the detailed content of How to use components in uniapp. For more information, please follow other related articles on the PHP Chinese website!