uniapp将异步api变成同步

<template>

<view class="content">

<image class="logo" src="/static/logo.png"></image>

<view class="text-area">

<text class="title">{{title}}</text>

</view>

</view>

</template>

<script>

import {

request

} from '@/tools/request/index.js'

export default {

data() {

return {

title: 'Hello',

}

},

onShow() {

this.fun1()

},

methods: {

async fun1() {

//等待执行

const rs = await this.fun2()

console.log(rs);

console.log('执行完成')

},

//耗时操作

fun2() {

return new Promise((resolve,reject) => {

uni.request({

url:'http://www.baidu.com',

success: (res) => {

resolve('ok')

},

fail: (err) => {

reject('err')

}

})

})

}

}

}

</script>

<style>

.content {

display: flex;

flex-direction: column;

align-items: center;

justify-content: center;

}

.logo {

height: 200rpx;

width: 200rpx;

margin-top: 200rpx;

margin-left: auto;

margin-right: auto;

margin-bottom: 50rpx;

}

.text-area {

display: flex;

justify-content: center;

}

.title {

font-size: 36rpx;

color: #8f8f94;

}

.red {

color: red;

}

.blue {

color: blue;

}

</style>

如果是showModal

示例

return new Promise( async resolve=> {

uni.showModal({

title:'title',

content:'content',

success: (res) => {

resolve(res)

}

})

})

相关推荐
im_AMBER1 分钟前
前端性能优化之首屏提速
前端·学习·性能优化
lxh011310 分钟前
计算右侧小于当前元素的个数 题解
javascript·数据结构·算法
天天向上102417 分钟前
vue 大屏适配的一种实现思路
前端·javascript·vue.js
SuperEugene18 分钟前
Vue/Vite 多环境配置实战:dev、test、prod 差异区分与避坑指南|Vue 工程化篇
前端·javascript·vue.js
结网的兔子27 分钟前
前端学习笔记(实战准备篇)——用vite构建一个项目【吐血整理】
前端·学习·elementui·npm·node.js·vue
kyriewen29 分钟前
盒模型:CSS 世界的物理法则,margin 塌陷与 padding 的恩怨情仇
前端·css·html
lichenyang45329 分钟前
React 性能优化组件设计模式与通信
前端·javascript·设计模式
小成C29 分钟前
别再把 Claude Code 用乱了:CLAUDE.md、Rules、Skills、Hooks 到底怎么分工?
前端·人工智能·面试
巫山老妖34 分钟前
OpenClaw 技术教程大全:从安装到多 Agent 协作,全在这里
java·前端
weixin_4462608535 分钟前
提升开发效率的神器!快速选择编码上下文 — React Grab
前端·react.js·前端框架