首页 > 其他分享 >vue2 数组18 some erver filter reduce axios

vue2 数组18 some erver filter reduce axios

时间:2022-12-01 18:02:09浏览次数:46  
标签:flex axios default reduce item vue2 font type

some:

 return  true是固定写法,终止some循环

 

erver:

 

filter:

 

 

 

优化写法:
 arr.filter(item=>item.state).reduce((累加的结果,当前循环项)=>{},初始值)

拿上面的arr优化写法示例:
arr.filter(item=>item.state).reduce((amt,item)=>{
  return amt += item.price*item.count
},0)

  reduce:

二次简写:
const count = arr.filter(item=>item.state).reduce((amt,item)=>amt += item.price*item.count,0)

  axios:

导入:
npm i axios -S
使用:
methods:{     // 封装请求列表数据     async initCar(){       // 调用axios的get方法数据       const {data:res}=await axios.get('接口')       console.log(res);     }   },   created(){     this.initCar()   }   案例:    <Goods v-for="item in list" :key="item.id" :title="item.goods_name" :img="item.goods_img" :Price="item.goods_price"  :state="item.goods_state"> </Goods>     import Goods from './components/Goods/Goods.vue' import axios from 'axios'     components: {     Header, Goods   },   data() {     return {       // 储存购物车的列表数据,默认空数组       list: [],     }   }, methods: {     // 封装请求列表数据     async initCar() {       // 调用axios的get方法数据       const { data: res } = await axios.get('https://www.escook.cn/api/cart')       // 请求到的数据需要渲染,则必须要转存到data上       console.log(res);       if (res.status === 200) {         this.list = res.list       }     }   },   created() {     this.initCar()     console.log(this.list);   }  Title组件: <template>   <div class="header-container">{{title}}</div> </template>
<script> export default {   props:{     title:{       default:'',       type:String,     }   } } </script>
<style lang="less" scoped> .header-container {   font-size: 12px;   height: 45px;   width: 100%;   background-color: #1d7bff;   display: flex;   justify-content: center;   align-items: center;   color: #fff;   position: fixed;   top: 0;   z-index: 999; } </style> Goods 组件: <template>   <div class="goods-container">     <!-- 左侧图片 -->     <div class="thumb">       <div class="custom-control custom-checkbox">         <!-- 复选框 -->         <input type="checkbox" class="custom-control-input" :id="'cb'  +  id" :checked="state" @change="statechange" />         <label class="custom-control-label" :for="'cb'  +  id">           <!-- 商品的缩略图 -->           <img :src="img" alt="" />         </label>       </div>     </div>     <!-- 右侧信息区域 -->     <div class="goods-info">       <!-- 商品标题 -->       <h6 class="goods-title">{{ title }}</h6>       <div class="goods-info-bottom">         <!-- 商品价格 -->         <span class="goods-price">¥{{ Price }}</span>         <!-- 商品的数量 -->       </div>     </div>   </div> </template>
<script> export default {   props: {     //在这里封装一个id是因为将来商品勾选状态变化后,需要子传父的形式,通知父组件,通知父组件,根据id修改对应商品的勾选状态     id: {       required: true,       type: true,     },     title: {       default: '',       type: String,     },     img: {       default: '',       type: String,     },     Price: {       default: '',       type: Number,     },     // 勾选状态     state: {       default: true,       type: Boolean,     },
  },   methods: {     statechange(e) {       const newState = e.target.checked       this.$emit('state-change', { id: this.id, value: newState })     }   }, } </script>
<style lang="less" scoped> .goods-container {   +.goods-container {     border-top: 1px solid #efefef;   }
  padding: 10px;   display: flex;
  .thumb {     display: flex;     align-items: center;
    img {       width: 100px;       height: 100px;       margin: 0 10px;     }   }
  .goods-info {     display: flex;     flex-direction: column;     justify-content: space-between;     flex: 1;
    .goods-title {       font-weight: bold;       font-size: 12px;     }
    .goods-info-bottom {       display: flex;       justify-content: space-between;
      .goods-price {         font-weight: bold;         color: red;         font-size: 13px;       }     }   } } </style> Footer组件: <template>   <div class="footer-container">     <!-- 左侧的全选 -->     <div class="custom-control custom-checkbox">       <input type="checkbox" class="custom-control-input" id="cbFull" :checked="isfull" @change="fullChange" />       <label class="custom-control-label" for="cbFull">全选</label>     </div>
    <!-- 中间的合计 -->     <div>       <span>合计:</span>       <span class="total-price">¥{{ amm }}</span>     </div>
    <!-- 结算按钮 -->     <button type="button" class="btn btn-primary btn-settle">结算({{ amm.toFixed(2) }})</button>   </div> </template>
<script> export default {   props:{     // 全选的状态     isfull:{       type:Boolean,       default:true,     },     amm:{       type:Number,       default:0,     }   },   methods:{     fullChange(e){       this.$emit('full-change',e.target.checked)     },   }
} </script>
<style lang="less" scoped> .footer-container {   font-size: 12px;   height: 50px;   width: 100%;   border-top: 1px solid #efefef;   position: fixed;   bottom: 0;   background-color: #fff;   display: flex;   justify-content: space-between;   align-items: center;   padding: 0 10px; }
.custom-checkbox {   display: flex;   align-items: center; }
#cbFull {   margin-right: 5px; }
.btn-settle {   height: 80%;   min-width: 110px;   border-radius: 25px;   font-size: 12px;     }
.total-price {   font-weight: bold;   font-size: 14px;   color: red; } </style>   Counter组件:

  

标签:flex,axios,default,reduce,item,vue2,font,type
From: https://www.cnblogs.com/wencaiguagua/p/16937853.html

相关文章

  • HDFS,MapReduce,Yarn 的架构思想和设计原理
        大家好,我是梦想家Alex。之前我也写了不少关于大数据技术组件的文章,例如:​​     前方高能|HDFS的架构,你吃透了吗?​​​​     MapReduce......
  • axios的config模式 工作中最常使用的一种方法
    axios的config模式工作中最常使用 语法:axios({}).then({}).catch({})(这里的语法不同之前的axios.('url地址').then({}).catch({)})<body><h2>axios的config模式......
  • form-axios提交数据
    form-axios提交数据<body><h2>ajax+form</h2><!--action提交的接口地址method提交的方法--><formaction="http://ajax-api.ith......
  • Vue2(笔记05) - Vue核心 - 计算属性
    计算属性需求1:两个文本框,输入内容后再合并显示在元素中;插值语法实现:<divid="root">姓:<inputtype="text"v-model:value="firstName"><br>名:<inputtype="text"v-......
  • hive sql语句转换成mapreduce
    hivesql语句转换成mapreduce 转:https://www.cnblogs.com/w-j-q/p/14863034.html#autoid-2-5-01.hive是什么?2.MapReduce框架实现SQL基本操作的原理是什么?3.Hive怎......
  • vue2源码分析- 数据响应简单模拟
    工作中大部分项目使用vue2做,但一直局限于使用,终于有闲暇时间可以学习下源码,网上优秀的源码分析很多,此文章只是记录个人所学,有问题欢迎大家指出,欢迎讨论,互相学习。下面是我......
  • 浏览器和服务器之间的通信 ajax axios 接口 接口文档
    浏览器和服务器通讯过程以咱们用的最多的浏览器为例,和服务器通讯的过程就像聊微信?浏览器:输入URL地址-->请求服务器:接收请求并返回对应的资源-->响应浏览器:......
  • axios 的请求方式
    1.绑定点击事件get请求无参数</head><body><h2>get请求+参数换地</h2><buttonclass="btn1">测试</button><buttonclass="btn2">测试有参数</butt......
  • MapReduce经典论文翻译(中英对照)
    MapReduce:SimplifiedDataProcessingonLargeClusters(MapReduce:简化大型集群下的数据处理)转:谷歌MapReduce经典论文翻译(中英对照)-小熊餐馆-博客园(cnblogs.c......
  • vue2 ref17 $refs 操控dom
    在vue中不建议安装jquery在vue中操控dom可以用ref方法 每一个ref对象中都包含一个$refs对象,组件的$refs对象都指向一个空对象<button@click="clg"class="red">打印<......