小程序监听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>
相关推荐
xiaofeichaichai1 小时前
Webpack
前端·webpack·node.js
问心无愧05132 小时前
ctf show web入门111
android·前端·笔记
唐某人丶2 小时前
模型越来越强,我们还需要 Agent 工程吗?—— 从价值重估到 Harness 实践
前端·agent·ai编程
智码看视界2 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
JS菌2 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
excel4 小时前
HLS TS 文件损坏的元凶:Git 提交与拉取
前端
Aphasia3114 小时前
https连接传输流程
前端·面试
徐小夕4 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
threelab4 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器