uni-app 之 uni.request 网络请求API接口

uni-app 之 uni.request 网络请求API接口

image.png

复制代码
<template>
    <!-- vue2的<template>里必须要有一个盒子,不能有两个,这里的盒子就是 view-->
    <view>
        
        --- uni.request 网络请求API接口 ---
        <view>
            <!-- 免费的测试接口 -->
            <!-- https://dog.ceo/api/breeds/image/random -->
            <image :src="picurl" mode="aspectFill" @click="getpicurl"></image>
        </view>
        --- @click加个点击事件,因为接口是随机获取图片,所以每点一下就随机刷新个图片 ---
    </view>
</template>

<script>
    export default {
        data() {
            return {

                picurl: ""

            }
        },
        methods: {
            getpicurl() {
                uni.showLoading({
                    title: "加载中" // 加个进度条
                })

                uni.request({
                    url: "https://dog.ceo/api/breeds/image/random",
                    success: res => {
                        console.log(res) // log打印获取的数据
                        this.picurl = res.data.message
                        uni.hideLoading() // 图片加载出来后,关闭进度条
                    }
                })
            }

        },
        onLoad() {
            // uni.request({
            //  url: "https://dog.ceo/api/breeds/image/random",
            //  success: res => {
            //      console.log(res) // log打印获取的数据
            //      this.picurl = res.data.message
            //  }
            // })
            this.getpicurl()
        }
    }
</script>

<style lang="scss">
</style>
相关推荐
南境十里·墨染春水几秒前
C++传记 this指针 及区分静态非静态成员(面向对象)
开发语言·jvm·c++·笔记
揽月凡尘几秒前
基于 SWIG 的 C++ Embind 绑定自动化技术说明
开发语言·c++·自动化
斌味代码5 分钟前
Vue3源码解读(一):响应式系统 reactive/ref 核心原理图解(2026最新版)
前端·javascript·vue.js
武藤一雄9 分钟前
C# 中精准锁定类型信息指南:typeof vs GetType()
开发语言·windows·c#·.net·.netcore
IAUTOMOBILE10 分钟前
Qt 入门级开发实践:浅析基于 QTtest 项目的 C++ GUI 编程基础
开发语言·c++·qt
yhole12 分钟前
Nginx解决前端跨域问题
运维·前端·nginx
凸头12 分钟前
从聊天机器人到业务执行者:Agentic Orchestration 如何重构 Java 后端体系
java·开发语言·重构
我爱学习好爱好爱13 分钟前
Ansible 常用模块详解:hostname、selinux 、file实战
前端·chrome·ansible
爱丽_13 分钟前
AQS 的 `state`:volatile + CAS 如何撑起原子性与可见性
java·前端·算法
zhuhezhang15 分钟前
一个用golang开发的文本对比工具
开发语言·后端·golang·wails