当前位置:   article > 正文

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

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

我的用的vue2.0 

# Vue 2 项目,安装 Vant 2:

npm i vant@latest-v2 -S

main.js

  1. import Vant from "vant"
  2. import 'vant/lib/index.css';
  3. Vue.use(Vant)
  1. <template>
  2. <div class="wrap">
  3. <!--type == 5是推广页是纯H5 contentType != 1 也是纯H5-->
  4. <video v-if="info.type != 5 && info.contentType == 1" :src="info.content" :poster="coverImage" controls class="video-style"></video>
  5. <div v-if="info.type != 5" class="title">
  6. <div class="d1">{{ info.name }}</div>
  7. <div class="d2">{{ info.createDate }}</div>
  8. </div>
  9. <div v-if="info.type != 5" class="share">
  10. <div class="create-by">{{info.createBy ? (info.createBy != 'admin' ? info.createBy : '管理员') : ''}}</div>
  11. </div>
  12. <!--真正使用的地方vant预览富文本里的图片-->
  13. <div v-html="info.brief" class="content" @click="judgeImg($event)"></div>
  14. </div>
  15. </template>
  16. <script>
  17. import { getTrainingDetail } from "@/api/course/training";
  18. import {ImagePreview} from "vant" //引入vant预览图片组件
  19. export default {
  20. metaInfo: {
  21. meta: [
  22. { charset: 'utf-8' },
  23. { name: 'viewport', content: 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no' }
  24. ]
  25. },
  26. name: "webview",
  27. data () {
  28. return {
  29. info: {},
  30. coverImage: ''
  31. };
  32. },
  33. watch: {
  34. $route: {
  35. handler: function (route) {
  36. this.redirect = route.query && route.query.redirect;
  37. },
  38. immediate: true
  39. }
  40. },
  41. created () {
  42. //console.log(this.$route.query.id)
  43. if(this.$route.query.id){
  44. this.handleView(this.$route.query.id)
  45. }
  46. },
  47. mounted() {
  48. document.title = '详情';
  49. },
  50. methods: {
  51. //判断是否为图片并提取当前图片地址
  52. judgeImg(e){
  53. if(e.target.tagName=="IMG" && e.target.currentSrc){
  54. this.previewSingleImg(e.target.currentSrc)
  55. }
  56. },
  57. //查看单个大图
  58. previewSingleImg(url) {
  59. ImagePreview({
  60. images: Array.of(url),
  61. startPosition: 0,
  62. });
  63. },
  64. handleView(id){
  65. getTrainingDetail(id).then(res => {
  66. if(res.code == 200){
  67. this.info = res.data
  68. if(this.info.name.length > 12){
  69. document.title = this.info.name.substring(0, 12);
  70. }else{
  71. document.title = this.info.name
  72. }
  73. this.coverImage = this.info.cover
  74. }
  75. });
  76. }
  77. }
  78. };
  79. </script>
  80. <style rel="stylesheet/scss" lang="scss" scoped>
  81. .video-style { display: block; width: 100%}
  82. .title{ padding: 10px; display: flex;}
  83. .title .d1{ flex: 1; color: #333; font-size: 18px; font-weight: bold;}
  84. .title .d2{ padding-top: 6px; white-space: nowrap; font-size: 12px; color: #999;}
  85. .content{ padding: 0 10px 40px 10px; font-size: 14px;}
  86. .share{ margin-top: 10px; padding-left: 10px}
  87. .create-by{ font-size: 14px; color: #333;}
  88. .content img{ max-width: 100%;}
  89. </style>

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/355554
推荐阅读
相关标签
  

闽ICP备14008679号