封装一个uniapp的全局弹窗组件,无需引入,全局使用

思路:

先写弹窗组件,在main.js中进行引入注册,然后新建一个这个组件的vue实例, 将这个vue实例挂载到全局this上面,通过调用全局事件总线,进行传值。

几个注意点:
 

第一步、编写.vue弹窗组件。

按照正常的公用组件的方式写一个弹窗组件就好

<template>
	<view class="smallDialog" v-if="inviteSHow || cpShow || roomSettingShow || homeShow" @tap="closeDialog">
		
	</view>
</template>

第二部、在mian.js中将该组件进行引入,方便全局使用该组件

import smallDialog from "@/components/liveComponents/smallDialog.vue" 
Vue.component('smallDialog', smallDialog)

第三步,使用$bus,进行数据的传递,从而触发组件

Vue.prototype.$bus = new Vue();

第四步,将全局弹窗组件实例挂载到根实例上 

我们应该如何操作我们第一步编写的,vue组件呢,我首先想到的是把这个组件放在app.vue里面,然后在组件上面绑定ref为modal,然而发现了报错,原来在uniapp中App.vue 本身不是页面,这里不能编写视图元素,也就是没有 <template> 。那就换一种别的方法:

2、对于H5页面以及小程序、app有不同的挂载方式,对于H5来说,用到了document,实现方法如下

// 创建一个新的 Vue 实例用于全局弹窗组件
 const dialogApp = new Vue({
	      render: h => h(smallDialog)
	    });
	    dialogApp.$mount();
	    document.body.appendChild(dialogApp.$el);
	    this.$smallDialog = dialogApp.$children[0];
	  
	    // 监听事件总线的 showSmallDialog 事件
	    this.$bus.$on('showSmallDialog', options => {
	      this.$smallDialog.show(options);
});

2、对于app以及小程序来说,无法使用document等获取dom,所以采用下面的方式

   // 其他平台的初始化逻辑
this.$smallDialog = this.selectComponent("#smallDialog");
   // 监听事件总线的 showSmallDialog 事件
this.$bus.$on('showSmallDialog', options => {
   this.$smallDialog.show(options);
});

3、main.js的完整代码

import Vue from 'vue'
import App from './App'
import store from './store' // store
import plugins from './plugins' // plugins
import './permission' // permission
import smallDialog from "@/components/liveComponents/smallDialog.vue" //组cp邀约组件

Vue.use(plugins)
Vue.component('smallDialog', smallDialog)

Vue.config.productionTip = false
Vue.prototype.$store = store

App.mpType = 'app'

const app = new Vue({
  ...App,
  mounted() {
	  if (process.env.VUE_APP_PLATFORM === 'h5') {
	    const dialogApp = new Vue({
	      render: h => h(smallDialog)
	    });
	    dialogApp.$mount();
	    document.body.appendChild(dialogApp.$el);
	    this.$smallDialog = dialogApp.$children[0];
	  
	    // 监听事件总线的 showSmallDialog 事件
	    this.$bus.$on('showSmallDialog', options => {
	      this.$smallDialog.show(options);
	    });
	  } else {
	    // 其他平台的初始化逻辑
	    this.$smallDialog = this.selectComponent("#smallDialog");
	    // 监听事件总线的 showSmallDialog 事件
	    this.$bus.$on('showSmallDialog', options => {
	      this.$smallDialog.show(options);
	    });
	  }
	  }
});

// 创建事件总线
Vue.prototype.$bus = new Vue();

app.$mount();

第五步,在弹窗组件上编写方法,用于改变值

show(options) {
				console.log("触发smalldialog组件")
			      // this.showDialog = true;
			      this.inviteShow = options.inviteShow || false;
			      this.cpShow = options.cpShow || false;
			      this.roomSettingShow = options.roomSettingShow || false;
			      this.homeShow = options.homeShow || false;
			      this.yaoqing = options.yaoqing || {};
			      this.personInfo = options.personInfo || {};
			    },

第六步,在其它页面上进行使用

bindCP() {
			
				this.$bus.$emit('showSmallDialog', {
					cpShow: true,
					yaoqing: {
					  user_avatar: 'path/to/avatar.jpg',
					  user_nick: 'User Nick',
					},
				  });
				  console.log("点击")

			},

整体思路:

根据提供的引用内容,uniapp全局弹窗组件可以通过以下步骤实现: 1.在项目中创建一个公共组件,可以命名为invite.vue组件的结构和样式可以参考提供的引用。 2.在main.js中导入invite.js,并安装插件Vue.use(invite)。 3.在需要弹出组件的任何组件内调用this.$openInvite()即可弹出组件,调用this.$closeInvite()即可关闭组件。 下面是一个范例代码,供参考: 1. invite.vue组件代码: ```html <template> <div class="invite-mask" v-show="isShow"> <div class="invite-container"> <div class="invite-header"> <span class="invite-title">邀请好友</span> <img class="invite-close" src="./static/images/close.png" @click="closeInvite" /> </div> <div class="invite-content"> <img class="invite-img" src="./static/images/invite.png" /> <p class="invite-text">邀请好友注册,双方都可获得优惠券</p> <button class="invite-btn" @click="closeInvite">知道了</button> </div> </div> </div> </template> <script> export default { data() { return { isShow: false } }, methods: { openInvite() { this.isShow = true }, closeInvite() { this.isShow = false } } } </script> <style> .invite-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); z-index: 999; } .invite-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 80%; max-width: 400px; background-color: #fff; border-radius: 10px; overflow: hidden; } .invite-header { display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f5f5f5; } .invite-title { font-size: 16px; font-weight: bold; } .invite-close { width: 20px; height: 20px; cursor: pointer; } .invite-content { display: flex; flex-direction: column; align-items: center; padding: 20px; } .invite-img { width: 80%; margin-bottom: 20px; } .invite-text { font-size: 14px; color: #666; margin-bottom: 20px; } .invite-btn { width: 80%; height: 40px; line-height: 40px; text-align: center; background-color: #ff9900; color: #fff; border-radius: 20px; cursor: pointer; } </style> ``` 2. invite.js代码: ```javascript import Invite from '@/components/invite' const install = function(Vue) { Vue.component('Invite', Invite) Vue.prototype.$openInvite = function() { this.$refs.invite.openInvite() } Vue.prototype.$closeInvite = function() { this.$refs.invite.closeInvite() } } export default { install } ``` 3. main.js代码: ```javascript import Vue from 'vue' import App from './App' import Invite from './utils/invite' Vue.use(Invite) Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ ...App }) app.$mount() ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值