前端当接口请求异步的时候 可以传个callback函数

在Vue的main.js文件中,通过异步接口请求数据,并在其他页面使用返回值。通过传递callback函数实现数据的跨页面交互。在实际应用中,可以添加判断确保数据获取后再进行操作。在小程序的图表渲染场景中,同样利用异步请求后的数据调用渲染方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有个需求是在vue 文件 main.js里面 请求接口 返回的值在其他页面上用

import { getOnlinekey } from "./api/common.js";
Vue.prototype.$GetOnlinekey=(callback)=>{
	getOnlinekey().then(res=>{
		let {code,result}=res.data
		console.log(result,"getOnlinekey")
		Vue.prototype.$KEY = {
			PROCESS_ID:result.design_ID, //租赁流程的流程ID
			CUSTOMER_CODE:result.sol_CUSTOMER ,//经销商表的Online表明
			SOL_HELP_CENTER:result.sol_HELP_CENTER						//帮助中心表 表明
		}
	})
	if(callback){
		callback()
	}
	
}
Vue.prototype.$GetOnlinekey()

这里传了个callback ()函数

在其他页面用的时候

	this.$GetOnlinekey(()=>{
					this.$Nav.go('/pages/form/form', {
						'processId': this.$KEY.PROCESS_ID,
						title: '项目申请'
					})
				})

或者加个判断

if(this.$KEY.PROCESS_ID){
this.$Nav.go('/pages/form/form', {
						'processId': this.$KEY.PROCESS_ID,
						title: '项目申请'
					})
}

还有一种情况是小程序的

场景是小程序的图表在请求完接口拿到数据后调用渲染的方法

//获取三天的数据
   getcaculateTime (callback)  {
     var that=this
     wx.request({
      url: prodm+'powerstation/caculateTime',
      method: "get",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        date: (that.data.nowTime).replace(/\./g, '-')
      },
      success:function (data) {
        console.log("三天的点",data)
        let data_X = data.data.object
        let temp_x = data_X.map(item => ({datetime:item}))
        that.setData({
          tempDataX:temp_x
        })
        callback()
      }
    })
   
  },
   this.getcaculateTime(()=>{   //这些都是画图的方法
      this.getCurrentData()    
      this.getVoltageData()
      this.getdaylypower()
      this.getWeekEnergy()
      this.getInverterTemper()
      this.getExchangeCurren()
      this.getExchangeVoll()
    })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值