vue3中做文件預覽的項目實踐
以vue3為基礎,針對常見文件類型做項目內的預覽功能
涉及到的文件類型有docx,xlsx,pdf,txt,png,jpg,jpeg,mp4,mp3,
這里你可能會疑惑為什么docx,xlsx都支持,為啥不順帶支持下doc和docx呢???
這里我也表示很難啊,針對老版本格式的文檔,沒有合適的解決方案,如果說一定要有,那就只能是通過下載的方式本地預覽了 ┭┮﹏┭┮
開整開整~
1. docx文檔預覽解決方案
安裝下依賴這里呢我用的是1.6.2版本
npm i @vue-office/docx 或者 yarn add @vue-office/docx
模版代碼塊里面這里呢支持的屬性分別介紹下
- src:資源路徑http://116.228.88.26:52800/nymanagerFile/group1/M00/00/B8/rBwWM2aGQyCAGisxAAqcqJw-Cq468.docx
- style:自定義樣式
- rendered:渲染完成后調用
- error:渲染失敗后調用
<vue-office-docx :src="previewSrc" :style="comStyle" @rendered="renderedHandler" @error="errorHandler" />
script 標簽內
<script setup> // 獲取父組件傳遞的資源url const props = defineProps({ previewSrc: { type: String, required: false, default: () => '' } }); //引入VueOfficeDocx組件相關 import VueOfficeDocx from '@vue-office/docx' import '@vue-office/docx/lib/index.css' </script>
運行成功之后展示
2. xlsx文檔預覽解決方案
安裝下依賴這里呢我用的是1.7.8版本
npm i @vue-office/excel 或者 yarn add @vue-office/excel
模版代碼塊里面這里呢支持的屬性分別介紹下
- src:資源路徑http://116.228.88.26:52800/nymanagerFile/group1/M00/00/B8/rBwWM2aGQyCAGisxAAqcqJw-Cq468.xlsx
- style:自定義樣式
- rendered:渲染完成后調用
- error:渲染失敗后調用
<vue-office-excel :src="previewSrc" :style="comStyle" @rendered="renderedHandler" @error="errorHandler" />
script 標簽內
<script setup> // 獲取父組件傳遞的資源url const props = defineProps({ previewSrc: { type: String, required: false, default: () => '' } }); //引入VueOfficeExcel組件相關 import VueOfficeExcel from '@vue-office/excel' import '@vue-office/excel/lib/index.css' </script>
運行成功之后展示
3. pdf&txt文檔預覽解決方案
無需安裝依賴這里呢采用的是iframe方式來展示,因為呢iframe是自帶支持pdf和txt的
模版代碼塊里面這里呢支持的屬性分別介紹下
- src:資源路徑http://116.228.88.26:52800/nymanagerFile/group1/M00/00/B8/rBwWM2aGQyCAGisxAAqcqJw-Cq468.pdf
<iframe :src="previewSrc" ></iframe>
script 標簽內
<script setup> // 獲取父組件傳遞的資源url const props = defineProps({ previewSrc: { type: String, required: false, default: () => '' } }); </script>
運行成功之后展示
4. video文檔預覽解決方案
安裝下依賴這里呢我用的是1.3.2版本
npm i vue3-video-play 或者 yarn add vue3-video-play
可以選擇在入口文件里面導入一下
// 視頻播放器組件 import App from './App' const app = createApp(App) import vue3videoPlay from 'vue3-video-play' // 引入組件 import 'vue3-video-play/dist/style.css' // 引入css app.use(vue3videoPlay)
這里導入的時候回存在一個問題,就是依賴包引入的路徑回有問題,會導致項目在運行的時候報錯。這里給出解決方案======》需要將依賴包中package.json文件中的’module’改為"./dist/index.mjs"
模版代碼塊里面
<vue3VideoPlay width="100%" title="鋼鐵俠" :src="previewSrc" :poster="options.poster" @play="onPlay" @pause="onPause" @timeupdate="onTimeupdate" @canplay="onCanplay" />
script 標簽內
<script setup> <script setup> import { reactive } from 'vue'; const props = defineProps({ previewSrc: { type: String, required: false, default: () => '' }, }); console.log('資源地址:',props.previewSrc); const options = reactive({ // src: "https://cdn.jsdelivr.net/gh/xdlumia/files/video-play/IronMan.mp4", //視頻源 poster: '', //封面 }) const onPlay = (ev) => { // console.log('播放') } const onPause = (ev) => { // console.log(ev, '暫停') } const onTimeupdate = (ev) => { // console.log(ev, '時間更新') } const onCanplay = (ev) => { // console.log(ev, '可以播放') } </script>
運行成功之后展示
5. audio(MP3等)文檔預覽解決方案
這里呢無需要裝第三方依賴,使用audio標簽
**模版代碼塊里面** ```javascript <audio controls controlsList="nodownload" style="width: 100%;" > <source :src="previewSrc" type="audio/mp3" > 您的瀏覽器不支持audio標簽。 </audio>
script 標簽內
<script setup> // 獲取父組件傳遞的資源url const props = defineProps({ previewSrc: { type: String, required: false, default: () => '' } }); </script>
運行成功之后展示
到此這篇關于vue3中做文件預覽的項目實踐的文章就介紹到這了,更多相關vue3 文件預覽內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在vue中使用el-tab-pane v-show/v-if無效的解決
這篇文章主要介紹了在vue中使用el-tab-pane v-show/v-if無效的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08vue使用websocket實現(xiàn)實時數(shù)據(jù)推送功能
這篇文章主要為大家詳細介紹了vue如何使用websocket實現(xiàn)實時數(shù)據(jù)推送,發(fā)布訂閱重連單點登錄功能,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12vue中利用mqtt服務端實現(xiàn)即時通訊的步驟記錄
前些日子了解到mqtt這樣一個協(xié)議,可以在web上達到即時通訊的效果,所以下面這篇文章主要給大家介紹了關于vue中如何利用mqtt服務端實現(xiàn)即時通訊的相關資料,需要的朋友可以參考下2021-07-07vue實現(xiàn)動態(tài)給id賦值,點擊事件獲取當前點擊的元素的id操作
這篇文章主要介紹了vue實現(xiàn)動態(tài)給id賦值,點擊事件獲取當前點擊的元素的id操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11