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)

}

})

})

相关推荐
鲨莎分不晴17 分钟前
【实战】老项目焕发新生:从 Webpack 平滑迁移到 Vite 避坑全记录
前端·webpack·node.js
web小白成长日记39 分钟前
从零起步,用TypeScript写一个Todo App:踩坑与收获分享
前端·javascript·typescript
圣心1 小时前
Gemini 模型 介绍
前端
huangyiyi666661 小时前
前端-远程多选搜索框不能反显的问题解决
前端·javascript·vue.js·笔记·学习
敲敲了个代码1 小时前
让 Vant 弹出层适配 Uniapp Webview 返回键
前端·javascript·vue.js·学习·面试·uni-app
AOwhisky1 小时前
Ansible管理变量和事实(管理变量部分) & 部署文件到受管主机
前端·chrome·ansible
摘星编程1 小时前
React Native for OpenHarmony 实战:Accessibility 辅助功能详解
javascript·react native·react.js
小二·2 小时前
Python Web 开发进阶实战:绿色软件工程 —— 构建低能耗、低碳排的可持续应用
开发语言·前端·python
hhcccchh2 小时前
学习vue第十四天 小白学父组件传递子组件(Props)
前端·vue.js·学习
派大鑫wink2 小时前
【Day32】Tomcat 服务器:安装、配置与 Web 应用部署
服务器·前端·tomcat