当前位置:网站首页>微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
2022-08-10 11:00:00 【夏天想】
我有个遮罩层有个点击隐藏的按钮,他在很多页面都出现这个遮罩,怎么保证在其中一个页面点击了隐藏,其他的页面都不会出现了?



我在三个tab页面都显示了这个关注公众号的遮罩层,但是在其中一个 页面点击后面的删除,其他的页面也不显示了.
把它写成一个公共的组件。在组件中监听,两个生命周期
lifetimes://生命周期函数,可以为函数,或一个在 methods 段中定义的方法名 pageLifetimes:// 组件所在页面的生命周期函数两个生命周期都要把当前全局的变量赋值给
app。js里面定义showOfficial
globalData: {
showOfficial:true,//是否显示关注公众号的bar
},official-bar.js
const app = getApp();
Component({
/**
* 组件的属性列表
*/
properties: {},
/**
* 组件的初始数据
*/
data: {
show:true,
checkIcon: 'https://wechat/static/wechat/check.png',
deleteIcon: 'https://wechat/static/wechat/deleteIcon.png',
},
lifetimes: {
attached: function() {
this.setData({
show:app.globalData.showOfficial
})
},
},
pageLifetimes: {
show: function() {
this.setData({
show:app.globalData.showOfficial
})
// 页面被展示
},
},
/**
* 组件的方法列表
*/
methods: {
//关注
clickMask() {
wx.navigateTo({
url: "/pages_subpack/officialAccount/officialAccount"
})
},
//隐藏当前的遮罩
delBar() {
app.globalData.showOfficial=false;//改变全局变量的值
this.setData({
show: false
})
},
}
})official-bar.wxml
<view class='modal-mask' wx:if="{
{show}}" catchtap='clickMask'>
<view class='modal-content'>
<image src="{
{checkIcon}}" class="check-icon"></image>
<view>关注公众号,上课自动提醒</view>
<button class="payAttention" type="primary" block>立即关注</button>
<view class="delete-area" catchtap="delBar">
<image src="{
{deleteIcon}}" class="delete-icon"></image>
</view>
</view>
</view>official-bar.wxss
.modal-mask{
display: flex;
justify-content: center;
align-items: center;
position: fixed;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.6);
z-index: 99;
height:76rpx;
}
/*遮罩内容*/
.modal-content{
color:#fff;
display: flex;
justify-content:space-evenly;
align-items: center;
text-align: center;
font-size:32rpx;
padding-left: 20rpx;
width: 100%;
}
/*中间内容*/
.main-content{
flex: 1;
height: 100%;
overflow-y: hidden;
max-height: 80vh; /* 内容高度最高80vh 以免内容太多溢出*/
}
.check-icon{
padding-right:18rpx;
width:32rpx;
height:32rpx;
}
.delete-icon{
width:38rpx;
height:38rpx;
}
.delete-area{
height: 69rpx;
line-height: 86rpx;
padding: 0 20rpx;
}
.payAttention{
background-color: #EB8207 !important;
border:none;
border-radius: 76rpx;
width: 160rpx;
height:52rpx;
line-height:52rpx;
font-size:28rpx;
padding:0;
margin: 0 0 0 20rpx;
}json里面引入
"official-bar": "/components/official-bar"
想要使用的页面
<official-bar></official-bar>
边栏推荐
猜你喜欢

从脚本到剪辑,影像大师亲授的后期制作秘籍

AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)

老板加薪!看我做的WPF Loading!!!

关于振弦采集模块及采集仪振弦频率值准确率的问题

AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)

Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了

1-IMU参数解析以及选择

Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户

From the product dimension, why can't we fully trust Layer2?

STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
随机推荐
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
Store limited time seckill function system
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
Centos7环境使用Mysql离线安装包安装Mysql5.7
三相380V整流后的电压
第5章相似矩阵及二次型(4)
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
学长告诉我,大厂MySQL都是通过SSH连接的
老板加薪!看我做的WPF Loading!!!
js对象转FormData对象(一般用于上传)
电脑怎么设置屏幕息屏时间(日常使用分享)
振弦传感器及核心VM系列振弦采集模块
快速上手,征服三种不同分布式架构调用方案
3款不同类型的自媒体免费工具,有效提高创作、运营效率
POJ 3101 Astronomy (Mathematics)
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
CPU多级缓存与缓存一致性
TCP/IP笔记
谷歌数据中心发生“电力事故”造成 3 人受伤