关于VUE3的数据

接口请求出来的数据一般为对象类型的

这里有三种方法存储数据:

  1. ref

  1. reactive([])不太推荐

  1. reactive([])嵌套一个对象去存储,推荐

1、ref

<template>

<van-swipe :autoplay="3000"lazy-render>

<van-swipe-itemv-for="(image,index) in images" :key="index">

<img :src="image.pic"/>

</van-swipe-item>

</van-swipe>

</template>

<script>

import{onMounted,reactive,ref} from 'vue'

import{postBanner} from '@/api/index'

export default {

setup() {

let images = ref([])

onMounted(async()=>{

getSwipeAPI()

})

async function getSwipeAPI(){

images.value = await postBanner(1)

}

console.log(images);

return { images };

},

}

</script>

2、reactive([]) 通过push存储,不推荐

<template>

<divv-for="(image,index1) in images3" :key="index1">

<van-swipe :autoplay="3000"lazy-render>

<van-swipe-itemv-for="(i,index) in image" :key="index">

<img :src="i" />

</van-swipe-item>

</van-swipe>

</div>

</template>

<script>

import{onMounted,reactive,ref} from 'vue'

import {postBanner}from '@/api/index'

export default {

setup() {

let images3 = reactive([])

onMounted(async()=>{

getSwipeAPI()

})

async function getSwipeAPI(){

let x = await postBanner(1)

x = x.map(v=>v.pic)

images3.push(x)

}

console.log(images3);

return {images3};

},

}

</script>

3、reactive([])嵌套对象

<template>

<van-swipe :autoplay="3000"lazy-render>

<van-swipe-itemv-for="(image,index) in images2.list" :key="index">

<img :src="image.pic"/>

</van-swipe-item>

</van-swipe>

</template>

<script>

import{onMounted,reactive,ref} from 'vue'

import{postBanner} from '@/api/index'

export default {

setup() {

let images2 = reactive({

list:[]

})

onMounted(async()=>{

getSwipeAPI()

})

async function getSwipeAPI(){

images2.list = await postBanner(1)

}

return {images2};

},

}

</script>

推荐使用reactive()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏天的狗子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值