Plover 发表于 2024-4-24 02:01:05

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

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

Plover 发表于 2024-4-24 12:00:48

有大佬知道怎么搞吗求回复

vivo官方技术团队 发表于 2024-4-25 16:49:00

<p>希望这个示例对你有帮助


// 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 === &#39;undefined&#39;) {
&nbsp; &nbsp; &nbsp;this.subscribers = [];
&nbsp; &nbsp;}
&nbsp; &nbsp;this.subscribers.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;
&nbsp; &nbsp;for (let i = 0; i &lt; subscribers.length; i++) {
&nbsp; &nbsp; &nbsp;if (action === &#39;publish&#39;) {
&nbsp; &nbsp; &nbsp; &nbsp;subscribers(arg);
&nbsp; &nbsp; &nbsp;} else {
&nbsp; &nbsp; &nbsp; &nbsp;if (subscribers === 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; }</p>

vivo官方技术团队 发表于 2024-4-25 16:54:19

<p>希望这个示例对你有帮助 // 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 === &#39;undefined&#39;) { &nbsp; &nbsp; &nbsp;this.subscribers = []; &nbsp; &nbsp;} &nbsp; &nbsp;this.subscribers.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; &nbsp; &nbsp;for (let i = 0; i &lt; subscribers.length; i++) { &nbsp; &nbsp; &nbsp;if (action === &#39;publish&#39;) { &nbsp; &nbsp; &nbsp; &nbsp;subscribers(arg); &nbsp; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp;if (subscribers === 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; }</p>
页: [1]
查看完整版本: app.ux异步数据在其他页面获取问题, 求大佬解答-官方