小程序监听globalData值改变

通过使用 eventBus 来监听

eventBus.js

javascript 复制代码
import Vue from 'vue';
export const EventBus = new Vue();

app.vue

xml 复制代码
<script>
	import {
		EventBus
	} from './utils/eventBus.js';
	export default {
	globalData: {
	   spid: 0
	},
	methods: {

	},
	onLoad() {
	    console.log('onoad')
	},
	onLaunch: function() {
	let that = this;
	setInterval(() => {
	     that.globalData.spid++;
	     EventBus.$emit('spidChanged', that.globalData.spid); // Emit event
	   }, 1000);
	},
	onShow: function() {
	  console.log('App Show')
	},
	onHide: function() {
	   console.log('App Hide')
	}
}
</script>

<style>
	/*每个页面公共css */
</style>

其他页面使用

xml 复制代码
<template>
  <div>
    <p>Current spid: {{ spid }}</p>
  </div>
</template>

<script>
import { EventBus } from './eventBus';

export default {
  data() {
    return {
      spid: 0
    };
  },
  created() {
    EventBus.$on('spidChanged', (newSpid) => {
      this.spid = newSpid;
    });
  },
  beforeDestroy() {
    EventBus.$off('spidChanged'); // Clean up the listener
  }
}
</script>
相关推荐
神奇的程序员1 小时前
我的软件冲进苹果商店下载榜前 50 了
前端
阳光是sunny2 小时前
别再被 worktree 绕晕了!AI 编程时代你必须掌握的 Git 隔离神器
前端·人工智能·后端
万少3 小时前
万少的博客 - 技术分享与解决方案
前端·javascript·后端
尘世中一位迷途小书童6 小时前
用 Cesium 撸了一个森林火情监控大屏,弧线、粒子、发光效果都齐了
前端·javascript
IT_陈寒6 小时前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
月光下的丝瓜7 小时前
Flutter 国内安装指南
前端·flutter
玄星啊7 小时前
AI 编程的第 30 天,我怀念古法 Coding 了
前端·ai编程
Jolyne_7 小时前
Angular基础速通
前端·angular.js
锋行天下8 小时前
半秒开!还有谁!!!
前端·vue.js·架构
代码搬运媛9 小时前
git 下中文文件名乱码问题解决
前端