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>
相关推荐
小羊失眠啦.几秒前
深入解析Rust的所有权系统:告别空指针和数据竞争
开发语言·后端·rust
Dxxyyyy1 分钟前
零基础学JAVA--Day32(ArrayList底层+Vector+LinkedList)
java·开发语言
nvd1110 分钟前
python 后端流式处理 LLM 响应数据详解
开发语言·python
蓝天智能12 分钟前
Qt 的字节序转换
开发语言·qt
CS_浮鱼42 分钟前
【C++进阶】智能指针
开发语言·c++
怕什么真理无穷1 小时前
C++_面试题_21_字符串操作
java·开发语言·c++
Fantasydg1 小时前
Request Response对象
前端
Wect1 小时前
学习React-DnD:核心组件与Hooks
前端
humors2211 小时前
前端开发案例(不定期更新)
前端·vue.js·elementui·ruoyi·若依
菠萝+冰1 小时前
npm中-d -g 和默认安装的区别
前端·npm·node.js