前端组件设计模板之小程序message消息通知

用于轻量级反馈或提示,不会打断用户操作。(组件描述

何时使用

当需要对用户进行较轻量的反馈或提示,可以自动消失或通过点击关闭,通常由用户触发。

场景收集

提示场景 提醒UI 功能点 文案/逻辑
截图提示 类型图标+文本、自定义消失时间 content:"xxx"、消失时间:3s
其他场景
业务逻辑:

这里可以写上你的业务逻辑

组件设计

xmind设计组件字段,可以定义类型

流程图

可以画一个流程图简单描述一下调用方法会执行哪些逻辑,组件内会有哪些方法、变量等

  • 父级组件引入组件
  • 在需要使用的地方,调用组件方法,显示提示/关闭提示
  • 调用show方法,会执行组件内部的add方法,添加到messageList数组中
  • 添加的逻辑
  • 添加后,会调用内部showMessage方法,设置组件visible为true,显示组件
  • 同时,setTimeout执行延迟后消失逻辑
  • 这样大致描述就可以

组件部分方法

可以结合部分的实验代码表明可行性

message/func

typescript 复制代码
// 获取到引入的message组件实例,调用show方法可直接渲染组件
const getInstance = function (callback, selector?: string) {
  const pages = getCurrentPages();
  const page: any = pages[pages.length - 1];
  const context = page.$$basePage || page;
  const instance = context ? context.selectComponent(selector || '#wx-message') : undefined;
  if (instance) {
    callback(instance);
    return instance;
  }
  console.error('未找到组件,请确认 selector && context 是否正确');
};

export default {
  show(options) {
    // 如果是使用了uniapp,需要用$vm,否则,可以不用.$vm
    return getInstance((instance) => instance.$vm.setMessage?.(options));
  },
  destroy() {
    return getInstance((instance) => instance.$vm.destroyMessage?.());
  },
  close() {
    return getInstance((instance) => instance.$vm.hideMessage?.());
  }
};

message/index

kotlin 复制代码
import { Vue, Component, Prop } from 'vue-property-decorator';
import {ICurrentMessage, MessageType} from './types';

@Component
export default class MediaVideo extends Vue {
  @Prop({ default: '' }) url;

  visible = false;
  messageList = []; // 消息列表
  closeTimeout = 0; // 延时关闭句柄 
  currentMessage: ICurrentMessage = {
    type: MessageType.info,
    content: '',
    duration: 3000,
    icon: '',
    zIndex: 1500,
    className: ''
  };

  /**
   * 设置,外部调用
   */
  setMessage(data) {
    this.addMessage(data);
  }

  /**
   * 添加消息队列
   * @param options 
   */
  addMessage(options) {
    this.messageList.push(options);
    this.showMessageItem(this.messageList[0]);
  }

  /**
   * 展示消息
   * @param item 
   */
  showMessageItem(item) {
    this.visible = true;
    this.currentMessage = {...this.currentMessage, ...item};
    if(this.currentMessage.duration > 0) {
      this.closeTimeout = setTimeout(() => {
        this.hideMessage();
      }, this.currentMessage.duration);
    }
  }

  /**
   * 关闭消息
   */
  hideMessage() {
    this.visible = false;
    this.closeTimeout && clearTimeout(this.closeTimeout);
  }

  /**
   * 销毁消息,清空队列
   */
  destroyMessage() {
    this.messageList = [];
    this.hideMessage();
  }

  beforeDestroy() {
    this.destroyMessage();
  }

}

代码演示

页面组件,引入Message

python 复制代码
import Message from '@/message'

@Component({
  components: {
    Message,
  }
})

<message id="wx-message"/>

使用组件

php 复制代码
import message from '@/message/func';

message.show({
    type: 'info',
    content: '这是内容',
    duration: 2000,
    onClose: () => {...}
})

API

名称 类型 默认值 说明 是否必传
type MessageType: info normal info N
content String / Slot - 用于自定义消息弹出内容 Y
duration number 3000 自动关闭的延时,单位秒。设置0不自动关闭 N
其他的字段名称...

全局方法

提供销毁方法

  • message.destroy()
  • message.close(key)

参考组件:tdesign.tencent.com/miniprogram...

组件代码:github.com/Tencent/tde...

相关推荐
菜冬眠。1 小时前
uni-app/微信小程序接入腾讯位置服务地图选点插件
前端·微信小程序·uni-app
依辰6 小时前
可观测性升级:小程序错误监控体系实践
前端·javascript·微信小程序
小兔崽子去哪了7 小时前
微信小程序入门
前端·vue.js·微信小程序
七月十二7 小时前
[微信小程序]对接sse接口
前端·微信小程序
mosen86810 小时前
【微信小程序】报错: http://127.0.0.1:7001 不在以下 request 合法域名列表中
微信小程序·小程序
Luostir13 小时前
在uniapp中实现富文本的编辑,上传与回显
前端·微信小程序
程序猿John13 小时前
微信小程序-下拉滚动加载数据
微信小程序·小程序
番茄Salad13 小时前
微信小程序中实现某个样式值setData改变时从350rpx到200rpx的平滑过渡效果
微信小程序·小程序
若川14 小时前
Taro 4 已发布:11. Taro 是如何解析入口配置 app.config.ts 和页面配置的?
前端·javascript·微信小程序
老李不敲代码19 小时前
榕壹云无人共享系统:基于SpringBoot+MySQL+UniApp的物联网共享解决方案
spring boot·物联网·mysql·微信小程序·uni-app·软件需求