基于element ui的图片预览插件

写插件很简单,满足两个条件即可,一、基本的逻辑思路,二、熟悉插件语法要求。本次Vue插件也比较简单,点击“查看图片”用轮播的方式限制用户上传的图片,如图:

项目采用的是vue-element-admin

在‘src/components’下新建‘imgPreview’文件夹,然后在该文件夹下新建‘ImgPreview.vue’,‘index.js’两个文件,代码如下:

ImgPreivew.vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<template>

  <el-dialog

    :visible.sync="isShowImageDialog"

    @closed="clearImg"

  >

    <el-carousel indicator-position="outside" height="600px">

      <el-carousel-item v-for="src in imgs">

        <img :src="src" style="max-width: 100%;max-height: 100%;display: block; margin: 0 auto;"/>

      </el-carousel-item>

    </el-carousel>

  </el-dialog>

</template>

<script>

export default {

  name: 'ImgPreview',

  data() {

    return {

      imgs: '',

      isShowImageDialog: false

    }

  },

  methods: {

    clearImg() {

      this.imgs = null

    }

  }

}

</script>

<style scoped>

</style>

  

index.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import ImgPreviewComponent from './ImgPreview'

const ImgPreview = {}

ImgPreview.install = Vue => {

  const ImgConstructor = Vue.extend(ImgPreviewComponent)

  const instance = new ImgConstructor()

  instance.$mount(document.createElement('div'))

  document.body.appendChild(instance.$el)

  Vue.prototype.$imgPreview = (e) => {

    instance.target = e.currentTarget

    instance.imgs = instance.target.getAttribute('data-img').split(',')

    instance.isShowImageDialog = true

  }

}

export default ImgPreview

  

应用也很简单:

main.js

1

2

3

import imgPreview from '@/components/imgPreview'

Vue.use(imgPreview)

  

comments.vue页面调用

1

2

3

4

5

6

7

8

9

<el-table-column

        label="评论内容"

        header-align="center"

      >

        <template slot-scope="scope">

          <div v-html="scope.row.content"></div>

          <el-button v-if="scope.row.images.length>0" :data-img="scope.row.images" type="text" size="small" @click="$imgPreview">查看图片</el-button>

        </template>

      </el-table-column>

  

整个插件原理也很简单:所有的数据都放在data-img上,插件获取其中的数据,然后通过element ui的el-carousel组件轮播显示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值