≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

vue图片上传本地预览组件使用分析

人气:675 时间:2019-04-15

这篇文章主要为大家详细介绍了vue图片上传本地预览组件使用分析,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

最近项目一直在使用vue,以前只是用vue做过一些简单的demo对数据进行增删改,并没有用于实际开发项目。今天就想了解一下如何用vue实现常见的图片上传前本地预览效果。

效果预览:

【图片暂缺】

代码如下:


<template>
 <div class="image-view">
  <div class="addbox">
   <input type="file" @change="getImgBase()">
   <div class="addbtn">+</div>
  </div>
  <div class="view">
   <div class="item" v-for="(item, index) in imgBase64">
    <span class="cancel-btn" @click="delImg(index)">x</span>
    <img :src="item">
   </div>
  </div>
 </div>
</template>
<script>
 export default {
  name: 'imageView',
  data (){
   return {
    imgBase64:[] //存储img base64的值将值传给后端处理
   }
  },
  methods: {
   //获取图片base64实现预览
   getImgBase(){
    var _this = this;
    var event = event || window.event;
    var file = event.target.files[0];
    var reader = new FileReader(); 
    //转base64
    reader.onload = function(e) {
     _this.imgBase64.push(e.target.result);
    }
    reader.readAsDataURL(file);
   },
   //删除图片
   delImg(index){
    this.imgBase64.splice(index,1);
   }
  }
 }
</script>
<style scoped>
 *{margin:0 auto;padding:0;font-family:"微软雅黑";}
 .clearboth::after{
  content:"";
  display:block;
  clear:both;
 }
 .image-view{
  width:400px;
  height:300px;
  margin:50px auto;
 }
 .image-view .addbox{
  float:left;
  position:relative;
  height:100px;
  width:100px;
  margin-bottom:20px;
  text-align:center;
 }
 .image-view .addbox input{
  position:absolute;
  left:0;
  height:100px;
  width:100px;
  opacity:0;
 }
 .image-view .addbox .addbtn{
  float:left;
  height:100px;
  width:100px;
  line-height:100px;
  color:#fff;
  font-size:40px;
  background:#ccc;
  border-radius:10px;
 }
 .image-view .item {
  position:relative;
  float:left;
  height:100px;
  width:100px;
  margin:10px 10px 0 0;
 }
 .image-view .item .cancel-btn{
  position:absolute;
  right:0;
  top:0;
  display:block;
  width:20px;
  height:20px;
  color:#fff;
  line-height:20px;
  text-align:center;
  background:red;
  border-radius:10px;
  cursor:pointer;
 }
 .image-view .item img{
  width:100%;
  height:100%;
 }
 .image-view .view{
  clear:both;
 }
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持四海网。

本文来自:http://www.q1010.com/184/6890-0.html

注:关于vue图片上传本地预览组件使用分析的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vue实现点击隐藏与显示实例分享
  • Vue结合后台导入导出Excel问题分析
  • 9102年webpack4搭建vue项目的方法步骤
  • Vue动态生成el-checkbox点击无法赋值的解决方法
  • 图文分析vue框架安装步骤
  • Vue中CSS动画原理的实现
  • vue中各种通信传值方式总结
  • vue实现的微信机器人聊天功能案例【附源码下载】
  • 分析vue几种主动刷新的方法总结
  • vue实现的网易云音乐在线播放和下载功能案例
上一篇:利用vue重构有赞商城的思路以及总结整理
下一篇:Vue动态生成el-checkbox点击无法赋值的解决方法
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。