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>
相关推荐
张赐荣几秒前
深入详解在 Python 中用 ctypes 调用 Windows API 清空回收站
开发语言·windows·python
费曼学习法2 分钟前
快速选择算法:如何在 10 亿数据中瞬间找到“第 K 大”?
javascript·算法
漫游的渔夫2 分钟前
前端开发者做 RAG:别只收集点赞点踩,用 6 个字段把反馈变成优化闭环
前端·人工智能·typescript
用户962377954484 分钟前
原理分析 | Controller —— SpringBoot 内存马
javascript·后端
彷徨而立4 分钟前
【C/C++】在头文件中定义全局变量的方法
c语言·开发语言·c++
Simon_52015 分钟前
Vue props传入function时的this指向问题_vue props function-CSDN博客
前端
写代码的皮筏艇17 分钟前
replace方法
前端·javascript
C澒17 分钟前
AI 生码 - API2CODE:接口智能匹配与 API 自动化生码实践
前端·低代码·ai编程
我命由我1234518 分钟前
Android 广播 - 显式广播与隐式广播
android·java·开发语言·java-ee·kotlin·android studio·android-studio