vue 预览v-html 富文本里的图片 使用vant

我的用的vue2.0

Vue 2 项目,安装 Vant 2:

javascript 复制代码
npm i vant@latest-v2 -S

main.js

javascript 复制代码
import Vant from "vant"
import 'vant/lib/index.css';
Vue.use(Vant)
javascript 复制代码
<template>
  <div class="wrap">
    <!--type == 5是推广页是纯H5 contentType != 1 也是纯H5-->
    <video v-if="info.type != 5 && info.contentType == 1" :src="info.content" :poster="coverImage" controls class="video-style"></video>   
    <div v-if="info.type != 5" class="title">
      <div class="d1">{{ info.name }}</div>
      <div class="d2">{{ info.createDate }}</div>
    </div>
    <div v-if="info.type != 5" class="share">
      <div class="create-by">{{info.createBy ? (info.createBy != 'admin' ? info.createBy : '管理员') : ''}}</div>
    </div>
    <!--真正使用的地方vant预览富文本里的图片-->
    <div v-html="info.brief" class="content" @click="judgeImg($event)"></div> 
  </div>
</template>

<script>
import { getTrainingDetail } from "@/api/course/training";
import {ImagePreview} from "vant"  //引入vant预览图片组件

export default {
  metaInfo: {
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no' }
    ]
  },
  name: "webview",
  data () {
    return {
      info: {},
      coverImage: ''      
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created () {
    //console.log(this.$route.query.id)
    if(this.$route.query.id){
      this.handleView(this.$route.query.id)      
    }    
  },
  mounted() {
    document.title = '详情';
  },
  methods: {
    //判断是否为图片并提取当前图片地址
    judgeImg(e){
        if(e.target.tagName=="IMG" && e.target.currentSrc){
            this.previewSingleImg(e.target.currentSrc)
        }
    },
    //查看单个大图
    previewSingleImg(url) {
        ImagePreview({
            images: Array.of(url),
            startPosition: 0,
        });
    },

    handleView(id){
      getTrainingDetail(id).then(res => {
        if(res.code == 200){
          this.info = res.data
          if(this.info.name.length > 12){
            document.title = this.info.name.substring(0, 12);
          }else{
            document.title = this.info.name
          }
          this.coverImage = this.info.cover
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.video-style { display: block; width: 100%}
.title{ padding: 10px; display: flex;}
.title .d1{ flex: 1; color: #333; font-size: 18px; font-weight: bold;}
.title .d2{ padding-top: 6px; white-space: nowrap; font-size: 12px; color: #999;}
.content{ padding: 0 10px 40px 10px; font-size: 14px;}
.share{ margin-top: 10px; padding-left: 10px}
.create-by{ font-size: 14px; color: #333;}
.content img{ max-width: 100%;}
</style>
相关推荐
安冬的码畜日常1 小时前
【D3.js in Action 3 精译_029】3.5 给 D3 条形图加注图表标签(上)
开发语言·前端·javascript·信息可视化·数据可视化·d3.js
太阳花ˉ1 小时前
html+css+js实现step进度条效果
javascript·css·html
程序员大金2 小时前
基于SpringBoot+Vue+MySQL的装修公司管理系统
vue.js·spring boot·mysql
john_hjy2 小时前
11. 异步编程
运维·服务器·javascript
风清扬_jd2 小时前
Chromium 中JavaScript Fetch API接口c++代码实现(二)
javascript·c++·chrome
yanlele2 小时前
前瞻 - 盘点 ES2025 已经定稿的语法规范
前端·javascript·代码规范
It'sMyGo3 小时前
Javascript数组研究09_Array.prototype[Symbol.unscopables]
开发语言·javascript·原型模式
xgq3 小时前
使用File System Access API 直接读写本地文件
前端·javascript·面试
李是啥也不会3 小时前
数组的概念
javascript