Code前端首页关于Code前端联系我们

百度小程序开发中页面通信问题的解决方案

terry 2年前 (2023-09-23) 阅读数 69 #移动小程序

关于小程序开发中页面通信问题,在支付情况下,用户从支付登录页面跳转到支付后,又回到首页,其中相关状态需要更新,例如用户的个人资料状态、付款状态、文档或产品状态。

随机问题

在百度小程序中使用swan.navigateBack跳转回页面时,API中的方法参数不支持回车参数,仅支持数字参数。

因此,不同页面之间存在通信问题。下面列出几种方法,供参考。 Swan 否

1返回页数 如果增量超过现有页数,则返回首页 1。 成功功能否- 接口回调函数呼叫成功。

方案一:使用app.js设置公共变量

使用app.js的公共功能设置APP上的变量。当

// app.js 启动文件
App({
    globalData: {
        isLogin: false,
        userInfo: null,
        networkError: false,
        networkType: 'none'
    }
})

用在其他页面时,使用:

// test.js
const app = getApp();
const commonParams = app.globalData.isLogin;

,但是错误也很明显。当数据量较大、数据之间的关系复杂时,维护起来会比较困难,逻辑也比较混乱。

方案二:使用存储

使用小程序的全局存储来访问数据。原理同方案一。

// 存储-异步
swan.setStorage({
    key: 'key',
    data: 'value'
});
// 存储-同步
swan.setStorageSync('key', 'value');

// 获取-异步
swan.getStorage({
    key: 'key',
    success: function (res) {
        console.log(res.data);
    },
    fail: function (err) {
        console.log('错误码:' + err.errCode);
        console.log('错误信息:' + err.errMsg);
    }
});

// 获取-同步
const result = swan.getStorageSync('key');

方案三:使用操作中心

使用操作中心进行注册发布。

// event.js 事件中心

class Event {
    on(event, fn, ctx) {
        if (typeof fn !== 'function') {
            console.error('fn must be a function');
            return;
        }

        this._stores = this._stores || {};
        (this._stores[event] = this._stores[event] || []).push({
            cb: fn,
            ctx: ctx
        });
    }
    emit(event, ...args) {
        this._stores = this._stores || {};
        let store = this._stores[event];
        if (store) {
            store = store.slice(0);
            for (let i = 0, len = store.length; i < len; i++) {
                store[i].cb.apply(store[i].ctx, args);
            }
        }
    }
    off(event, fn) {
        this._stores = this._stores || {};
        // all
        if (!arguments.length) {
            this._stores = {};
            return;
        }
        // specific event
        let store = this._stores[event];
        if (!store) {
            return;
        }
        // remove all handlers
        if (arguments.length === 1) {
            delete this._stores[event];
            return;
        }
        // remove specific handler
        let cb;
        for (let i = 0, len = store.length; i < len; i++) {
            cb = store[i].cb;
            if (cb === fn) {
                store.splice(i, 1);
                break;
            }
        }
        return;
    }
}

module.exports = Event;

在app.js中声明和管理

// app.js
import Event from './utils/event';

App({
    event: new Event()
})

在注册页面,使用on方法进行注册

// view.js 阅读页进行订阅

Page({
    // 页面在回退时,会调用onShow方法
    onShow() {
        // 支付成功的回调,调起下载弹层
        app.event.on('afterPaySuccess', this.afterPaySuccess, this);
    },
    afterPaySuccess(e) {
        // ....业务逻辑
    }
})

在发布页面,根据状态营销发出

// paySuccess.js

const app = getApp();

app.event.emit('afterPaySuccess', {
    docId: this.data.tradeInfo.docId,
    triggerFrom: 'docCashierBack'
});

根据发布预订活动中心,通过感知页面之间的交互,可以实现支付成功后滚动页面时改变页面状态的情况。同时,还需要维护页面之间的关系。通过发布时传递参数,即可执行数据。沟通。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门