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

栏目: JavaScript · 发布时间: 7年前

内容简介:今天记录几个简单的小问题,前端时间开发用到的,之前看到博客中没有记录,简单记录一下。 一个是element上传组件循环引用的方式,一个是简单的倒计时。上传组件每个上传都要指定相应的函数,而且函数不能传入参数,10个上传按钮要写10个上传函数,非常麻烦。针对这个,我们可以循环这些函数。element一个上传组件如下:

前言

今天记录几个简单的小问题,前端时间开发用到的,之前看到博客中没有记录,简单记录一下。 一个是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)

小结

简单的小案例就分享到这里,明天十月一国庆节了,haorooms祝大家国庆节快乐,玩的开心!


以上所述就是小编给大家介绍的《element上传组件循环引用及简单时间倒计时》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

PHP for the World Wide Web, Second Edition (Visual QuickStart Gu

Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99

So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具