vue2富文本实现图片查看

<template>
	<div>
		<div v-html="content"></div>
		<el-image-viewer
	      v-if="showViewer"
	      :on-close="closeViewer"
	      :url-list="contentPic"
	      :z-index="9999"
	      />
	</div>
</template>

JS代码

import ElImageViewer from 'element-ui/packages/image/src/image-viewer';、
components: { ElImageViewer },//引入element组件
 getHtmlPic(val) {
            if (!val) return;
            const srcList = [];
            this.content = val.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
                srcList.push(capture); // capture图片地址  match img标签整体
                return `<img src=${capture} class="content-img" style="width:100%;display:block">`; //给图片添加class方便后面取dom
            });
            this.$nextTick(() => {
                const imgHtml = document.getElementsByClassName('content-img');
                for (let i = 0; i < imgHtml.length; i += 1) {
                    imgHtml[i].onclick = () => {
                        //解决点击预览时不是当前图片问题
                        const tempImgList = [...srcList]; // 所有图片地址
                        if (i === 0) {
                            this.contentPic = tempImgList;
                        } else {
                            // 调整图片顺序,把当前图片放在第一位
                            const start = tempImgList.splice(i);
                            const remain = tempImgList.splice(0, i);
                            this.contentPic = start.concat(remain);
                        }
                        this.onPreview();
                    };
                }
            });
        },

   onPreview() {
            this.showViewer = true;
        },
        closeViewer() {
            this.showViewer = false;
        },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用vue-quill-editor或vue2-editor来实现富文本编辑器。下面是使用vue-quill-editor的步骤: 1.安装vue-quill-editor:npm install vue-quill-editor --save 2.在需要使用富文本编辑器的组件中引入vue-quill-editor:import VueQuillEditor from 'vue-quill-editor' 3.注册VueQuillEditor组件:Vue.component('vue-quill-editor', VueQuillEditor) 4.在模板中使用VueQuillEditor组件:<vue-quill-editor v-model="content"></vue-quill-editor> 5.在data中定义content变量,用于双向绑定富文本编辑器的内容。 6.如果需要自定义图片上传功能,可以使用以下代码: <vue-quill-editor v-model="content" :options="editorOption"></vue-quill-editor> data() { return { content: '', editorOption: { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['link', 'image', 'video'] ], imageDrop: true, imageResize: {}, imageExtend: { loading: false, headers: { Authorization: 'Bearer ' + localStorage.getItem('token') }, params: {}, action: '/api/upload/image', responseFn: function (res) { return res.data.url } } } } } } 以上是使用vue-quill-editor实现富文本编辑器的步骤,你可以根据自己的需求进行定制化开发。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值