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 小时前
Vue2或Vue3项目引用百度地图
javascript·vue.js·typescript·前端框架·html5
兆子龙2 小时前
深入 ahooks 3.0 useRequest 源码:插件化架构的精妙设计
javascript·面试
电商API_180079052473 小时前
淘宝商品详情数据获取全方案分享
开发语言·前端·javascript
前端不开发3 小时前
在任意网页里“召唤”一个火柴人:一次有趣的 JavaScript Hack
前端·javascript
UI设计兰亭妙微3 小时前
资金运营管理监控系统交互设计、UI设计及VUE开发
vue.js·ui·交互
王子肥波3 小时前
🚀 TabTab Admin:一款现代化的 Vue 3 后台管理系统模板
vue.js
白开水丶4 小时前
vue3源码学习(五)ref 、toRef、toRefs、proxyRefs 源码学习
前端·vue.js·学习
icebreaker5 小时前
Mokup:构建工具友好的可视化 Mock 工具
前端·javascript·后端
禾叙_5 小时前
【netty】Channel
开发语言·javascript·ecmascript
不想秃头的程序员5 小时前
Vue3 子传父全解析:从基础用法到实战避坑
前端·vue.js·面试