- class Page {constructor(opts) {
- // 用于基础 page 存储各种默认 ui 属性
- this.isLoadingShow = 'none';
- this.isToastShow = 'none';
- this.toastMessage = 'toast 提示';
- // 通用方法列表配置, 暂时约定用于点击
- this.methodSet = [
- 'onToastHide', 'showToast', 'hideToast', 'showLoading', 'hideLoading'
- ];
- // 当前 page 对象
- this.page = null;
- }
- initPage(pageData) {
- //debugger;
- let _pageData = {};
- // 为页面动态添加操作组件的方法
- Object.assign(_pageData, this.getPageFuncs(), pageData);
- // 生成真实的页面数据
- _pageData.data = {};
- Object.assign(_pageData.data, this.getPageData(), pageData.data || {});
- console.log(_pageData);
- return _pageData;
- }
- // 当关闭 toast 时触发的事件
- onToastHide(e) {
- this.hideToast();
- }
- // 设置页面可能使用的方法
- getPageFuncs() {
- let funcs = {};
- for (let i = 0, len = this.methodSet.length; i < len; i++ ) {
- funcs[this.methodSet[i]] = this[this.methodSet[i]];
- }
- return funcs;
- }
- // 产出页面组件需要的参数
- getPageData() {
- return {
- isLoadingShow: this.isLoadingShow,
- isToastShow: this.isToastShow,
- toastMessage: this.toastMessage
- }
- }
- showToast(message) {
- this.setData({
- isToastShow: '',
- toastMessage: message
- });
- }
- hideToast() {
- this.setData({
- isToastShow: 'none'
- });
- }
- // 需要传入 page 实例
- showLoading() {
- this.setData({
- isLoadingShow: ''
- });
- }
- // 关闭 loading
- hideLoading() {
- this.setData({
- isLoadingShow: 'none'
- });
- }
- }
- // 直接返回一个 UI 工具了类的实例
- module.exports = new Page
来源: https://www.cnblogs.com/yexiaochai/p/9393212.html