vue/vant获取富文本中的图片预览

40 篇文章 6 订阅

这是一个模板字符串的标签里面有很多张图片,我们想通过字符串渲染并且预览图片

var htmlString = `
	<p class="p1">
		<img src="https://i8.mifile.cn/v1/a1/9c3e29dc-151f-75cb-b0a5-c423a5d13926.webp">
		<img src="https://i8.mifile.cn/v1/a1/f442b971-379f-5030-68aa-3b0accb8c2b9.webp">
		<img src="https://i8.mifile.cn/v1/a1/63b700b6-643e-ec18-fdf3-da66b4b4173f.webp">
		<img src="https://i8.mifile.cn/v1/a1/e9dbf276-193e-11c4-99a6-3097fde82350.webp">
		<img src="https://i8.mifile.cn/v1/a1/1249d3ee-2990-a2b4-28d9-f719c2417b1f.webp">
		<img src="https://i8.mifile.cn/v1/a1/97c79915-64b2-808c-53b4-4345652a179f.webp">
		<img src="https://i8.mifile.cn/v1/a1/cd0fbe1e-a1b3-a87a-f4a6-6fb08ec54931.webp">
	</p>`;

通过v-html渲染标签元素

<div v-html="img" @click="imageChgange($event)"></div>
 img: "",

mouted中绑定这个变量

 this.img = htmlString;

效果:

重点来了!!

这里需要点击屏幕上的每张图片,点击后将只预览本图片,而且能对应上这个富文本的图片预览

一、给div设置点击事件,传一个$event

二、获取e.target.currentSrc路径

打印出e看一下是什么东西

我们就从e里面获取富文本中的路径也就是 e.target.currentSrc (获取当前点击后的图片路径)

 console.log(e.target.currentSrc);

打印看是否能找到正确的路径

 找到之后就好办了,接着往下看。

三、通过vant使用图片预览功能

vant中组件调用使用方法如下:

 

 需要引入一下vant中的图片预览插件

import { ImagePreview } from "vant";

      if (e.target.nodeName == "IMG") { //判断他的类型是不是img
        ImagePreview({
          images: [e.target.currentSrc], //e.target.currentSrc  当前图片src
          showIndex: false,
          loop: false,
        });
      } else {
        return;
      }

整段代码如下: 

    imageChgange(e) {
      console.log(e);
      console.log(e.target.currentSrc);
      if (e.target.nodeName == "IMG") {
        ImagePreview({
          images: [e.target.currentSrc], //e.target.currentSrc  当前图片src
          showIndex: false,
          loop: false,
        });
      } else {
        return;
      }
    },

 实现效果如下:

 

 

 

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue Quill Editor是一个Vue文本编辑器插件,它可以用于在Vue项目进行文本编辑。您可以通过安装Vue-Quill-Editor和Quill来使用它。 关于图片的问题,根据提供的引用内容,我没有找到直接关于文本图片的说明。但是,根据Vue Quill Editor的使用方法,您可以使用自定义的toolbar来实现相关功能。您可以在toolbar添加一个按钮,当用户点击该按钮时,触发一个方法来文本图片。您可以使用Vue的v-if指令根据条件来显示或隐藏图片的组件。具体实现的代码如下所示: ``` <template> <div class="editor"> ... <!-- 自定义toolbar增加图片按钮 --> <div id="my-toolbar"> ... <button @click="previewImages">图片</button> </div> ... </div> </template> <script setup> import { QuillEditor, Quill } from '@vueup/vue-quill' import { getCurrentInstance, ref } from "vue"; const { proxy } = getCurrentInstance(); const previewImages = () => { // 实现图片的逻辑 // 可以使用Quill的API获取文本图片,然后展示在组件 } </script> ``` 在上述代码,您可以在`previewImages`方法实现图片的逻辑。您可以使用Quill的API获取文本图片,并将其显示在组件。 需要注意的是,具体的图片逻辑和组件实现取决于您的具体需求和项目实现。您可以根据自己的情况进行适当的调整和扩展。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue-Quill-Editor文本编辑器的使用教程](https://download.csdn.net/download/weixin_38701952/13980065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [使用vue-quill展示文本内容](https://blog.csdn.net/qq_36576430/article/details/127793120)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Southern Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值