2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Method 1: Using a callback function
globalData
A callback function is executed after that. This callback function can be an update function passed by the page to the app.- // app.js
- App({
- globalData: {
- someData: '',
- },
- setSomeData(newData, callback) {
- this.globalData.someData = newData;
- if (typeof callback === 'function') {
- callback();
- }
- },
- })
In the subpage: Call where neededappInstance.setSomeData
And pass a callback function
- // pages/index/index.js
- Page({
- onLoad() {
- const appInstance = getApp();
- appInstance.setSomeData('new value', () => {
- this.setData({
- localData: appInstance.globalData.someData,
- });
- });
- },
- })
Create an event bus (EventBus)app.js
Trigger an event in the subpage and listen to this event in the subpage.