[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
关于API这东西,前端各个框架都是不同的,且都经常更新,不用死记,所以本篇博客记录一下Vue中业务上常用的API(稍复杂些的API,简单的略)
Vue中的数组操作
* 循环 <ul> // FAQ: key值一般不取index(数组下标值) 取唯一的id值
//(若一个页面有多个数组,index会有多个,key值则失去了唯一性) <li v-for="(item, index) in list" :key=
"item.id">{{item.name}}</li> <ul>
* 改变数组值 list:['a','b','c']; // 此为data中的数组 list[0] = 'aa'; //
此种修改是无效的,UI界面不会重新渲染 这是应该注意的 // 正确的姿势 通过调用 $set(array, index, value) // 传参:1.
要操作的数组 2. 下标值 3. 修改的值 this.$set(this.list, 0, 'aa'); // 或 Vue.$set(this.list,
0, 'aa');
* 对象数组元素交换位置(适用于业务上的,列表交换和上下移动) list:[ { id:1, name: '小明', }, { id:2, name:
'小红', }, { id:3, name: '小绿', }, ] // 上下移动 function changeSort(type, index){ let
tempArray; // 上移 if(type == 'up'){ // 搞个临时数组接收一下 tempArray = this.list[index-1];
// 交换 this.$set(this.list, index-1,this.list[index]); this.$set(this.list, index
, tempArray); }else{ tempArray = this.list[index + 1]; this.$set(this.list,
index+ 1,this.list[index]); this.$set(this.list, index, tempArray); } } //
随机调换也是一样的,传index就可以了 // ...代码略
* 删除数组元素 this.$delete(this.list, index); // index 下标值
Vue中的对象操作
obj: { name: '猪小明', age: 18, class: '三年二班' }
* 修改 this.$set(this.obj, 'name', '王八蛋'); // obj 中的 name 从 “朱小明” 变成了 “王八蛋”
* 删除 this.$delete(this.obj, "age"); // obj中的age没得了
Vue中的 $nextTick
这玩意太长,下次单独写一篇,放个链接(先去恰个羊肉火锅,告辞!)
本篇博客会不定时更新,记录Vue中各种常用的API和容易踩的坑,需要的同学可以收藏一波,如果对你有帮助,可以在评论区扣个“有用”,然后再点个赞,感谢感谢~