请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
搜索

技术分享 app.ux异步数据在其他页面获取问题, 求大佬解答-官方

3
回复
552
查看
[复制链接]

1

主题

2

帖子

15

积分

 楼主| 2024-4-24 02:01:05 显示全部楼层 |阅读模式

我需要在app.ux里面登录,但是都是异步, 在其他页面获取数据的时候为空, 获取不到,   怎么样才能在其他页面, 获取到app.ux里面异步方法执行完的数据呢? 官方就不能搞个同步吗,太麻烦了

回复

使用道具 举报

1

主题

2

帖子

15

积分

 楼主| 2024-4-24 12:00:48 显示全部楼层
有大佬知道怎么搞吗 求回复
回复

使用道具 举报

2024-4-25 16:49:00 显示全部楼层

希望这个示例对你有帮助 // app.ux (使用发布订阅模式) /** * 应用级别的配置,供所有页面公用 */ import axios from 'axios' const pubSub = {  subscribers: {    any: []  },  subscribe: function(fn, type) {    type = type || 'any';    if (typeof this.subscribers[type] === 'undefined') {      this.subscribers[type] = [];    }    this.subscribers[type].push(fn);  },  unsubscribe: function(fn, type) {    this.visitSubscribers('unsubscribe', fn, type);  },  publish: function(publication, type) {    this.visitSubscribers('publish', publication, type);  },  visitSubscribers: function(action, arg, type) {    const pubtype = type || 'any';    const subscribers = this.subscribers[pubtype];    for (let i = 0; i < subscribers.length; i++) {      if (action === 'publish') {        subscribers[i](arg);      } else {        if (subscribers[i] === arg) {          subscribers.splice(i, 1);        }      }    }  } }; /* @desc: 注入方法至全局 global,以便页面调用 */ const hook2global = global.__proto__ || global hook2global.$pubSub = pubSub export default { async onCreate() {        const data = await axios('https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&pageSize=30&isVerify=1&pageNo=1')        hook2global.$pubSub.publish(data)    } } // 其他页面使用app.ux里面的数据     export default {       onInit(){            $pubSub.subscribe(this.getData)       },            getData(data){           console.log(data)      }   }

回复

使用道具 举报

2024-4-25 16:54:19 显示全部楼层

  1. 希望这个示例对你有帮助 // app.ux (使用发布订阅模式) /** * 应用级别的配置,供所有页面公用 */ import axios from &#39;axios&#39; const pubSub = { &nbsp;subscribers: { &nbsp; &nbsp;any: [] &nbsp;}, &nbsp;subscribe: function(fn, type) { &nbsp; &nbsp;type = type || &#39;any&#39;; &nbsp; &nbsp;if (typeof this.subscribers[type] === &#39;undefined&#39;) { &nbsp; &nbsp; &nbsp;this.subscribers[type] = []; &nbsp; &nbsp;} &nbsp; &nbsp;this.subscribers[type].push(fn); &nbsp;}, &nbsp;unsubscribe: function(fn, type) { &nbsp; &nbsp;this.visitSubscribers(&#39;unsubscribe&#39;, fn, type); &nbsp;}, &nbsp;publish: function(publication, type) { &nbsp; &nbsp;this.visitSubscribers(&#39;publish&#39;, publication, type); &nbsp;}, &nbsp;visitSubscribers: function(action, arg, type) { &nbsp; &nbsp;const pubtype = type || &#39;any&#39;; &nbsp; &nbsp;const subscribers = this.subscribers[pubtype]; &nbsp; &nbsp;for (let i = 0; i &lt; subscribers.length; i++) { &nbsp; &nbsp; &nbsp;if (action === &#39;publish&#39;) { &nbsp; &nbsp; &nbsp; &nbsp;subscribers[i](arg); &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp;if (subscribers[i] === arg) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;subscribers.splice(i, 1); &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp;} &nbsp;} }; /* @desc: 注入方法至全局 global,以便页面调用 */ const hook2global = global.__proto__ || global hook2global.$pubSub = pubSub export default { async onCreate() { &nbsp; &nbsp; &nbsp; &nbsp;const data = await axios(&#39;https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&amp;provinceId=0&amp;pageSize=30&amp;isVerify=1&amp;pageNo=1&#39;) &nbsp; &nbsp; &nbsp; &nbsp;hook2global.$pubSub.publish(data) &nbsp; &nbsp;} } // 其他页面使用app.ux里面的数据 &nbsp; &nbsp; export default { &nbsp; &nbsp; &nbsp; onInit(){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$pubSub.subscribe(this.getData) &nbsp; &nbsp; &nbsp; }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getData(data){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data) &nbsp; &nbsp; &nbsp;} &nbsp; }
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册