- 直接代码
<img width="500" v-if="showImg" height="500" :src="url" />
<iframe v-else :src="url" width="100%" height="100%" frameborder="0"></iframe>
- 粘贴一下vue组件代码可直接使用
<template>
<el-dialog :title="title" :visible.sync="open" top="3vh" append-to-body :custom-class="`doc-view ${handleClass}`"
:width="width">
<div style="height: calc(100% - 100px);width: 100%;">
<img width="500" v-if="showImg" height="500" :src="url" />
<iframe v-else :src="url" width="100%" height="100%" frameborder="0"></iframe>
</div>
<div style="display: flex;justify-content:flex-end;margin-top: 10px;">
<slot></slot>
</div>
</el-dialog>
</template>
<script>
import website from "@/const/website.js";
export default {
data() {
return {
open: false,
width: "",
title: "",
url: "",
isWordSuffer: ["doc", "docx"],
isImgSuffer: ["png", "jpg", "jpeg"],
isPdfSuffer: ["pdf"],
isExcelSuffer: ["xls", "xlsx"],
showImg: false,
};
},
computed: {
handleClass() {
if (this.showImg) return "";
return "is_iframe";
},
},
methods: {
show(url, suffName = "jpg") {
const preview_url = website.ossUrl + url.slice(url.lastIndexOf("/"));
const { isWordSuffer, isImgSuffer, isPdfSuffer, isExcelSuffer } = this;
if (isImgSuffer.includes(suffName)) {
this.showImg = true;
this.title = "图片预览";
this.width = "600px";
this.url = preview_url;
} else {
this.showImg = false;
this.width = "960px";
if (isPdfSuffer.includes(suffName)) {
this.title = "PDF预览";
this.url = preview_url;
} else if (isWordSuffer.includes(suffName)) {
this.title = "Word预览";
this.url = `https://view.officeapps.live.com/op/view.aspx?src=${preview_url}`;
} else if (isExcelSuffer.includes(suffName)) {
this.width = "1200px";
this.title = "Excel预览";
this.url = `https://view.officeapps.live.com/op/view.aspx?src=${preview_url}`;
}
}
this.open = true;
},
},
};
</script>
<style lang="scss">
.doc-view {
.el-dialog__body {
}
}
.doc-view.is_iframe {
.el-dialog__body {
height: 75vh;
}
}
</style>