这两天都在看微信小程序开发,在看的过程当中,也会自己写写,有的时候,这个时候就会想到,以前项目当中如ReactJs里会有一些公共的组件好多地方都要用到,于就是想着微信小程序里如何提取出来。
在小程序里提取公共部分叫template。在其它页面上要用的时候,只要引用就可以了,下面就是如何创建一个的步骤吧,自己也标记一下。
由于我个人喜爱,我会将我所有的组件都会继承一个父类,之前C#经常这样用,将一些通用的方法什么都写到父类里去,子类直接调用父类方法,省此代码量
这里写代码片
<template name="XtnLoading">
<view class="loadingCss" bindtap="onCloseLoading" wx:if="{{IsShow===true}}">
<view class="content">
<image class="loading" src="/components/loading/svg/loading.svg" background-size="cover">image>
<view class="title">{{Title}}view>
view>
view>
template>
.loadingCss {
padding: 0px;
position: fixed;
top: 0px;
left: 0px;
background: rgba(0, 0, 0, 0.2);
height: 100%;
width: 100%;
display: -webkit-box;
-webkit-box-align: center;
-webkit-box-pack: center;
font-size: 16px;
}
.loadingCss .content {
padding: 20px;
border: 1px solid #fff;
border-radius: 10px;
background: #fff;
text-align: center;
}
.loadingCss .content .title {
text-align: center;
}
.loadingCss .content .loading {
width: 48px;
height: 48px;
}
import { Utility, BaseComponent } from '../Core';
class loading extends BaseComponent{
constructor() {
super();
}
}
const Loading = new loading();
export { Loading };
/**
* 初始化操作
*
* @memberof loading
*/
OnInit() {
// 调用父类方法
this.__Init(Utility);
// 添加事件监听
this.ListenerEvent();
this.data.TData = { Title: '加载中...', IsShow: false };
// 这人很重要,如果想让组件里有事件的话,必须把事件绑定到当前Page上去
// 下面就是将当前类中的事件,绑定到页面上。
this.__CurrentPage.onCloseLoading = this.onCloseLoading.bind(this);
this.__UpdateData();
}
/**
* 销毁事件,请在 页面 onHide 或 onUnload的时候,调用此方法
*
* @memberof loading
*/
OnDestroy() {
Object.keys(this.LoadingEventInfo || {}).forEach((key) => {
const value = this.LoadingEventInfo[key];
Utility.$RemoveEvent(key, value);
});
}
/**
* 更新数据,通知页面宣染用的
*
* @memberof loading
*/
__UpdateData() {
this.setData(this.data);
}
/**
* 监听事件方法
*
* @memberof loading
*/
ListenerEvent() {
const { onLoading, onLoadingHide } = Utility.$ConstItem.Events.ShowModel;
const self = this;
const __Update = (isShow) => {
this.data.TData.IsShow = isShow;
this.__UpdateData();
};
const LoadingIndex = Utility.$On(onLoading, () => {
__Update(true);
});
const LoadingHide = Utility.$On(onLoadingHide, () => {
__Update(false);
});
const EventInfo = {};
EventInfo[onLoading] = LoadingIndex;
EventInfo[onLoadingHide] = onLoadingHide;
this.LoadingEventInfo = EventInfo;
}
/**
* 关闭操作
*
* @memberof loading
*/
onCloseLoading() {
this.data.TData.IsShow = !this.data.TData.IsShow;
this.__UpdateData();
}
上面代码就一个组件基本上就OK了,里面用到了一个类 Utility,EventEmitter等,在这里就贴代码了,点击文件名称,就可能查看内容了。
<import src="../../components/loading/loading.wxml" />
引用了,可是在什么地方如何使用它呢?
<template is="XtnLoading" data="{{...TData}}"/>
or
<view>
<template is="XtnLoading" data="{{...TData}}"/>
view>
/**
* 弹出Loading组件,2秒后关闭
*
*/
onTapLoading() {
Utility.$Loading();
let times = 2;
this.data.LoadingTitle = '将在(' + times + ')后隐藏';
this.setData(this.data);
const Interval = setInterval(() => {
times--;
this.data.LoadingTitle = '将在(' + times + ')后隐藏';
this.setData(this.data);
if (times === 0) {
this.data.LoadingTitle = '点击显示Loading弹框';
Utility.$LoadingHide();
clearInterval(Interval);
}
}, 1000);
到这里一个组件就基本上完成好了。