uniapp 小程序注册全局弹窗组件(无需引入,无需写标签)

由于uniapp没有开放根节点,所以一般情况下通过app.components注册,在需要的页面直接写组件标签,但是如果每个页面都需要的话,再每个都加的话会非常的麻烦

网上的思路都不咋地:

1.通过写一个透明弹窗页面来实现,亲测页面透明设置无效。

2.通过编写全局组件每个页面都引入,非常不方便。


这里采用vite插件实现,这里满足uniapp+Vue3+vite 目前小程序测试没有问题

安装插件

npm i @yck-web/vite-plugin-template-inset 

 配置:

//修改配置vite.config.js, plugins中添加插件(位置需要放在uni()方法前面)
import uni from "@dcloudio/vite-plugin-uni";
  import vitePluginTemplateInset from "@yck-web/vite-plugin-template-inset";

  plugins: [
     vitePluginTemplateInset(['<GlobalDialog ref=\'GlobalDialog\'></GlobalDialog>']),
     uni(),
  ],
//main.js全局注册

import GlobalDialog from '@/components/GlobalDialog'

app.component('GlobalDialog', GlobalDialog)
<!-- 弹窗口组件 -->
<template name="globalDialog">
  <view class="mask position-fixed bg-#000 opacity-60% w-100vh h-100vh" style="z-index:1" v-if="show">
		mask
  </view>
</template>


<script setup lang="ts">
import { ref } from "vue";

let show = ref(false);

const open = () => {
  show.value = true;
};
const close = () => {
  show.value = false;
};


//暴露open和close方法
defineExpose({
  open,
  close,
});
</script>
<style lang="scss" scoped>

</style>
//页面使用
const instance = getCurrentInstance()
const showGlobalPop = () => {
		instance?.refs?.GlobalDialog?.open();
	}
根据提供的引用内容,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() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Goat恶霸詹姆斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值