为了解决uni-app 任意位置出现弹窗
受限于uni-app 调用组件需要每个页面都引入注册才可以使用,此方案繁琐,每个页面都要写侵入性比较强
app端:新建一个页面进行跳转,可以实现伪弹窗(其实是打开一个背景透明的页面)
web端: 全局挂载body 插入一个弹窗
就是 利用条件编译,web端写组件、app 端写页面,利用不同的生命周期,完成通用的逻辑
统一暴露一个show 方法,app 端使用 onload 监听事件触发,web端使用 show 方法触发
{{ params.title }}
{{ params.title }}
,{
"path" : "components/globalPopup/globalPopup",
"style": {
"navigationStyle": "custom",
"backgroundColor": "transparent",
"app-plus": {
"animationType": "fade-in",
"background": "transparent",
"popGesture": "none",
"bounce": "none",
"titleNView": false
}
}
},
globalPopup.js
const install = Vue => {
Vue.prototype.$globalPopup = {
show(params) {
let pointPageUrl = getCurrentPages()[getCurrentPages().length - 1].route;
if (pointPageUrl == 'components/globalPopup/globalPopup') return
uni.navigateTo({
url: '/components/globalPopup/globalPopup',
success: function (res) {
// 利用事件 通知 目标页面
res.eventChannel.emit('globalPopup', params)
}
})
}
}
}
export default install;
main.js 的编码 条件编译
// #ifdef APP-PLUS
import globalPopupjs from '@/components/globalPopup/globalPopup.js';
Vue.use(globalPopupjs);
// #endif
// #ifdef H5
import globalPopup from '@/components/globalPopup/globalPopup.vue'
const PopupVue = Vue.extend(globalPopup);
const popupDom = new PopupVue();
Vue.prototype.$globalPopup = popupDom.$mount();
const lastEl = document.body.lastElementChild;
if (lastEl.id !== 'globalPopup-box') {
setTimeout(() => {
document.body.appendChild(Vue.prototype.$globalPopup.$el)
}, 0)
}
// #endif
利用接口触发,返回相关弹窗配置
接口触发逻辑
if (data.pop) {
uni.$emit('showMyPopup', data.pop)
}
监听逻辑
// 监听事件
uni.$on('showMyPopup', (pop) => {
if (!this.isShowGlobalPopup) {
console.log(pop, 'showMyPopup')
let {
userQuestionStyleValue, // 样式值 1底部弹窗 2页中弹窗
userQuestionTemplateValue, // 模板值 1是否类 2打分类,
userQuestionInfo,
userQuestionAnswerDTO,
} = pop
this.$globalPopup.show({
id: pop.id,
show: true,
type: userQuestionTemplateValue == '1' ? 1 : 2,
userQuestionInfo,
title: userQuestionInfo.questionName,
userQuestionAnswerDTO,
mode: userQuestionStyleValue == '1' ? 'bottom' : 'center'
})
}
});