首页 > 编程 > JavaScript > 正文

element上传组件循环引用及简单时间倒计时的实现

2019-11-19 12:47:54
字体:
来源:转载
供稿:网友

前言

今天记录几个简单的小问题,前端时间开发用到的,之前看到博客中没有记录,简单记录一下。 一个是element上传组件循环引用的方式,一个是简单的倒计时。

上传组件每个上传都要指定相应的函数,而且函数不能传入参数,10个上传按钮要写10个上传函数,非常麻烦。针对这个,我们可以循环这些函数。

案例

element一个上传组件如下:

<el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i></el-upload>

假如有10个上传,岂不是要指定10个handleAvatarSuccess这个回掉函数?这些太麻烦了!!!

no! 我们可以不用这么写。推荐的一个写法如下:

<div class="pzsrltv" v-for="(item,index) in sValueAddedServiceData" :key="index"> <!--这一块循环出来 -->  <div class="s_step1">    <div class="stitle">{{item.name}}<span class="sblue" v-if="item.showimg" @click.stop="showImg.show = true;showImg.url = item.showimg">点击查看示例</span>    </div>    <div class="one_line">      <img class="imagelist" v-if="svalueImg[item.value]" :src="`${viewUrl}${svalueImg[item.value]}`" >      <el-upload      v-if="!svalueImg[item.value]"      class="avatar-uploader mt10"      accept="image/jpeg,image/png,image/gif"      :action="baseUpload"      :show-file-list="false"      :on-success="handlescSuccess[item.value]"      :before-upload="beforeAvatarUpload">      <i class="el-icon-plus avatar-uploader-icon"></i>      </el-upload>    </div>  </div> </div>

如上面代码,我们直接循环上传。

我们在data()里面指定handlescSuccess: {},

data(){ return { handlescSuccess: {}, svalueImg: {}, }}

初始化的时候,对上传进行设置

for (let i = 1; i <= 10; i++) { //循环的个数 this.handlescSuccess[i] = function(res, file) {  // console.log(res, _this.svalueImg)  if (_this.svalueImg) {   _this.$set(_this.svalueImg, i, res.file.sFile)  } }}

上面的代码是针对一个上传按钮只能上传一张图片的情况。上传多种做法类似。

例如如下:

//以下代码写在回调里面  for (let i = 0; i < item.iNum; i++) {   // 图文视频上传函数   _this.handleTWSuccess[`${i}`] = function(res, file) {    _this.sEvaluate['2'][i].sImg.push(res.file.sFile)   }  }

时间倒计时

这个实现起来很简单,但是在vue Dom 中实时展示,要用$set方式

天,小时,分钟,秒的倒计时函数:

data里面:

data(){ return { letTimes: { nowTime: '' }, }}

methods里面:

countDown(times) {   const _this = this   let timer = null   timer = setInterval(function() {    let day = 0,     hour = 0,     minute = 0,     second = 0// 时间默认值    if (times > 0) {     day = Math.floor(times / (60 * 60 * 24))     hour = Math.floor(times / (60 * 60)) - (day * 24)     minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60)     second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)    }    if (day <= 9) day = '0' + day    if (hour <= 9) hour = '0' + hour    if (minute <= 9) minute = '0' + minute    if (second <= 9) second = '0' + second    _this.$set(_this.letTimes, 'nowTime', `${day !== '00' ? `${day}天:` : ''}${hour}小时:${minute}分钟:${second}秒`)    times--   }, 1000)   if (times <= 0) {    _this.$set(_this.letTimes, 'nowTime', '')    clearInterval(timer)   }  },

单纯分钟和秒倒计时

function resetTime(time){ var timer=null; var t=time; var m=0; var s=0; m=Math.floor(t/60%60); m<10&&(m='0'+m); s=Math.floor(t%60); function countDown(){  s--;  s<10&&(s='0'+s);  if(s.length>=3){  s=59;  m="0"+(Number(m)-1);  }  if(m.length>=3){  m='00';  s='00';  clearInterval(timer);  }  console.log(m+"分钟"+s+"秒"); } timer=setInterval(countDown,1000);}

用法很简单,传秒数进来就可以了

例如:

this.countDown(5689)this.resetTime(256)

小结

简单的小案例就分享到这里,国庆愉快,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表