Write sub-components (take the pop-up box as an example)
xml
<view class="popbg" wx:if="{
{showPop}}">
<text>{
{
popText}}</text>
</view>
css:
.popbg{
width: calc(80% - 2rem);
margin: 2rem 10% 0;
padding: 0.7rem 1rem;
background: #fff;
border: 1px solid rgb(0,255,0);
text-align: center;
border-radius: 10rpx;
position: fixed;
top: 0;
left: 0;
z-index: 100000;
}
.popbg text{
color: rgb(15, 161, 40);
}
Add the following configuration to the json file on this page
{
"component":true
}
Edit the js part of the component
Component({
options: {
multipleSlots: true // 在组件定义时的选项中启用多slot支持
},
/**
- 页面的初始数据
*/
data: {
showPop:true
},
properties: {
popText: {
type: String,
value: '组件默认内容'
},
},
/**
- 在组件实例进入页面节点树时执行
*/
attached() {
var _this = this;
console.log(1111)
setTimeout(function () {
_this.setData({
showPop: false })
_this.handleClose()
}, 1000)
},
methods:{
handleClose(){
console.log("处理关闭内容")
var showStatus={
msg:'已成功关闭'}
this.triggerEvent('myevent', showStatus)
}
}
})
After the child component is written, the parent component is introduced
Write the parent component
First register in json
{
"usingComponents": {
"pop": "../components/pop/infoShow"
}
}
Multiple custom components can be introduced into one page. The writing method is as follows:
"usingComponents": { "pop": ".../components/pop/infoShow", "alert": ".../components/alert/alert" } You can also register global Components, "usingComponents" in app.json : { "pop": "…/components/pop/infoShow" }
Introduced in the xml file
<view class="container">
<button type="primary" catchtap="clikShowPop">点击</button>
<text style="margin-top:3rem;color:red" wx:if="{
{showTips}}">{
{
childMsg}}</text>
<pop id="popInfo" bind:myevent="showStatus" wx:if="{
{showPop}}" popText="{
{infoMsg}}"></pop>
</view>
The js part of the parent component:
Page({
data: {
infoMsg:'',
popInfo:'',
showPop:false,
showTips:false,
childMsg:''
},
//点击按钮事件
clikShowPop(){
this.setData({
showPop: true,
infoMsg:'点击成功',
})
},
//弹框消失时触发父组件的事件
showStatus(e){
// console.log(JSON.stringify(e))
this.setData({
showTips:true,
childMsg: e.detail.msg //接受子组件传递的参数时要用e.detail去接收
})
},
onLoad() {
this.popInfo = this.selectComponent('#popInfo') //获取组件对象,调用子组件内部函数时就可以用this.popInfo.handleClose()去调用了
}
})
So far, a simple custom component is encapsulated
Notes about custom components:
- Component Common page part is Page Custom component is Component
- options
- data: constants in the child component can be controlled by this.selectComponent('#popInfo')…setData({ showPop: false }) in the parent component
- properties is the value passed by the parent component to the child component
- attached is a life cycle function
- methods is the function encapsulated in the child component. The
child component can call this.triggerEvent('myevent', data) to trigger the function of the parent component, similar to this.$emit('myevents',data) in vue, data is passed data. Parent and child bind functions through bind:myevent="showStatus", which is
equivalent to @myevent="showStatus" in vue.
About the life cycle of components: For
more details, please see https://developers.weixin.qq.com/miniprogram/ dev/framework/custom-component/
will not be described in detail here