当前位置: 首页 > article >正文

uniapp上如何绑定全局事件总线(引入自定义全局组件例如弹窗)

1、在main.js中挂载bus

// main.js
Vue.prototype.$bus = new Vue();
uni.$bus = Vue.prototype.$bus;  // 确保在 uni 上绑定

2、写一个全局弹窗组件

<template>
  <view v-if="visible" class="toast-container">
    <view class="icon-container">
      <text class="icon">!</text> <!-- 圆形边框内的感叹号 -->
    </view>
    <text class="toast-message">{{ message }}</text>
  </view>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      message: ''
    };
  },
  created() {
    this.$bus.$on('showToast', this.showToast);
  },
  methods: {
    showToast(message) {
      this.message = message;
      this.visible = true;
      setTimeout(() => {
        this.visible = false;
      }, 2000); // 2秒后自动隐藏
    }
  }
};
</script>

<style scoped>
.toast-container {
  position: fixed;
  bottom: 150rpx;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  padding: 10px 20px;
  border-radius: 5px;
  text-align: center; /* 文字居中 */
  z-index: 9999;
}

.icon-container {
  width: 60rpx; /* 圆形的宽度 */
  height: 60rpx; /* 圆形的高度 */
  border: 2px solid white; /* 圆形的边框 */
  border-radius: 50%; /* 使其变为圆形 */
  display: flex; /* 使用 flexbox 居中内容 */
  align-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
  margin: 0 auto 5px; /* 圆形和文本之间的间距 */
}

.icon {
  color: white; /* 图标的颜色 */
  font-size: 24px; /* 图标的大小 */
}

.toast-message {
  color: #fff;
  font-size: 18px; /* 调整字体大小 */
}
</style>

3、需要的地方引入使用

uni.$bus.$emit('showToast', result.response.data.message);

也可以在easycom中学习uniapp组件的全局引入方式

"easycom": {
		"^u-(.*)": "@/uni_modules/uview-ui/components/u-$1/u-$1.vue",
        "^my-(.*)": "@/componets/my-$1.vue",
	}

 其中$1就是my-后面的变量值。


http://www.kler.cn/news/356228.html

相关文章:

  • Java中的Object类
  • 前端将cvs格式数据导出为Excel表
  • uniapp配置微信小程序分包(分包优化)
  • 代码随想录算法day43 | 图论part01 | 图论理论基础,深搜理论基础,98. 所有可达路径,广搜理论基础
  • 确保Spring Boot定时任务只执行一次方案
  • Git原理与使用(一)
  • 在Windows上搭建ChatTTS:从本地部署到远程AI音频生成全攻略
  • Realistic Car Controller Pro 车辆汽车控制
  • 软件设计模式------抽象工厂模式
  • 电脑桌面自己变成了英文Desktop,怎么改回中文
  • c语言求π的值
  • 微信小程序定位判断点位是否在某个范围内(腾讯地图转百度地图)
  • springboot032阿博图书馆管理系统(论文+源码)_kaic
  • 等保测评:如何进行有效的安全合规性审查
  • Dropout为何能防止过拟合?dropout和BN 在前向传播和方向传播阶段的区别?
  • 基于51单片机的proteus数字时钟仿真设计
  • c语言:整数的分解与提取and打印数根(手写代码版)
  • 2024年N1叉车司机证考试题库及N1叉车司机试题解析
  • 【业务场景】详谈电商优惠系统的设计与实现
  • 精选20个爆火的Python实战项目(含源码),直接拿走不谢!