vue项目 - uniapp 弹窗设置间隔时间 进入页面自动显示

需求:
首次进入首页出现弹窗,设置多久后再次显示时间(比如 24 小时) 则·在这个时间段内不会再显示弹窗,24小时之后,再次进入,则弹出弹窗

显示:
在这里插入图片描述
代码:

弹窗样式组件
<home-Pop-Up
      :ad="PopUpAd"  传值弹窗内数据
      :showFullAd="show['popUp']"  设置true 或者 false
      @close="close" 传值点击关闭事件
></home-Pop-Up>
data() {
    return {
    show: {
        popUp: false,  //设置初始值不显示
      },
    }
 },

methods: {
  close(name) {
    console.log("case", name);
    if (name === "popUp") {
        this.checkHomePopup();
    }
  },


checkHomePopup() {
      // console.log("再次进去,case");
    uni.getStorage({
        key: "popUp-show-time",
    success: (res) => {
          // console.log("case 第二次进入,checkHomePopup有");
          // 24小时超时
     if (
            !res.data ||
            new Date().getTime() - res.data > 1000 * 60 * 60 * 24
      ) {
            uni.setStorage({
              key: "popUp-show-time",
              data: new Date().getTime(),
      });
            // console.log("case 二次进入,超时,checkHomePopup");
            this.show.popUp = true;
      } else {
            console.log("case er   else");
      }
    },
    fail: (err) => {
          // console.log("case 创建。fail");
         uni.setStorage({ 
            key: "popUp-show-time",
            data: new Date().getTime(),
          });
      },
   });
},

openPopAd() {
  this.show.popUp = true;
     uni.setStorage({
        key: "popUp-show-time",
        data: new Date().getTime(),
  });
     // console.log("case 本地无,或超时重新创建");
},


getPopUp() {
   uni.getStorage({
      key: "popUp-show-time",
      success: (res) => {
          // 24小时超时
       if (
            !res.data ||
            new Date().getTime() - res.data > 1000 * 60 * 60 * 24
       ) {
            // console.log("刚进入,本地有,且超时", "case");
            this.show.popUp = true;
            this.openPopAd();
       } else {
            // console.log("刚进入,时间没到", "case");
            this.checkHomePopup();
          }
       },
       fail: (err) => {
          // console.log("刚进入,本地无", "case");
          this.openPopAd();
       },
  });
},
},
mounted() {
   this.getPopUp();
},

弹窗组件内部的设置

<ta-modal :value="showPopUp" :type="'custom'">
  <slot>
     <div>弹窗样式</div>
     <button class="close" @tap.stop="closePopUp">关闭弹窗</button>
   </slot>
</ta-modal>
  props: {
    ad: {
      type: Object,
      default() {
        return {};
      },
    },
    showFullAd: {
      type: Boolean,
      default: false,
    }
  },
  
data() {
    return {
      showPopUp: false,
    };
},

 methods: {
    closePopUp() {
      this.showPopUp = false;
      this.$emit("close","popUp");
      //弹窗组件内部 接收 close
    },
  },

  watch: {
    showFullAd() {
      this.showPopUp = this.showFullAd;
    },
  },
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue组件中调用弹窗组件可以通过以下步骤实现: 1. 首先,创建一个弹窗组件,可以在组件的`template`中定义弹窗的内容和样式。 ```vue <template> <div class="modal"> <div class="modal-content"> <!-- 弹窗内容 --> <slot></slot> </div> </div> </template> <script> export default { name: 'Modal', // ...组件的其他配置 } </script> <style> .modal { /* 弹窗样式 */ } .modal-content { /* 弹窗内容样式 */ } </style> ``` 2. 在需要使用弹窗的父组件中,引入并注册弹窗组件。 ```vue <template> <div> <!-- 点击按钮触发弹窗 --> <button @click="openModal">打开弹窗</button> <!-- 使用弹窗组件 --> <Modal v-if="showModal"> <!-- 弹窗内容 --> <h2>这是一个弹窗</h2> <p>欢迎使用弹窗组件!</p> <button @click="closeModal">关闭</button> </Modal> </div> </template> <script> import Modal from './Modal.vue'; export default { name: 'ParentComponent', components: { Modal, }, data() { return { showModal: false, }; }, methods: { openModal() { this.showModal = true; }, closeModal() { this.showModal = false; }, }, } </script> ``` 在上述代码中,点击按钮 `打开弹窗` 会触发 `openModal` 方法,将 `showModal` 属性设置为 `true`,从而显示弹窗组件。弹窗组件在父组件中使用时,可以通过插槽(slot)传递弹窗的内容和样式。 这样,当点击按钮时,弹窗组件会显示,并且可以通过弹窗中的按钮或其他交互元素来关闭弹窗

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王小王和他的小伙伴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值