網站首頁 編程語言 正文
react-native-doc-viewer文檔預覽
react-native項目要求實現word,excel,pdf,mp4,png等格式附件圖片的在線預覽,最終選用react-native-doc-viewer實現,
具體步驟如下
1、安裝react-native-doc-viewer
(1) npm install react-native-doc-viewer --save
(2)?react-native link react-native-doc-viewer
2、修改源代碼
(1) 因為react-native-doc-viewer已經很久沒有更新了,所以SdkVersion是23,比較低,將它修改成如下圖所示:
(2)找到下圖中左側的文件,將右側紅圈圈里面的代碼注釋掉
至此,所以安裝和配置完成,下面是寫代碼步驟
3、代碼實現
import OpenFile from 'react-native-doc-viewer';
//預覽附件
viewAttachment(attachment) {
let fileTypeArr = ['xls', 'ppt', 'doc', 'xlsx', 'pptx', 'docx', 'png', 'jpg', 'pdf', 'mp4', 'txt'];
if (fileTypeArr.indexOf(attachment.fileSuffix) >= 0) {
if (Platform.OS === 'ios') {
OpenFile.openDoc([{
url: attachment.filePath,
fileNameOptional: attachment.fileName
}], (error, url) => {
if (error) {
console.log(error);
} else {
console.log(url)
}
})
} else {
OpenFile.openDoc([{
url: attachment.filePath,
fileName: attachment.fileName,
cache: false,
fileType: attachment.fileSuffix
}], (error, url) => {
if (error) {
console.error(error);
} else {
console.log(url)
}
});
}
} else {
Toast.show('app端暫不支持此種格式附件預覽,請去pc端預覽');
}
}
應用會打開手機上對應的第三方軟件,做預覽。?
react-native初體驗的總結
一、前提知識點
- RN中文網鏈接 https://www.react-native.cn/docs/getting-started
- html、JavaScript、css基礎
- es6+等新的概念
- react、redux相關知識
- 如果需要用ts語法,還需要了解typescript相關知識點
- 數據請求,官方中有推薦的,我這里選擇axios
二、開發前后相關的一些配置
react-native相關知識點可以參考官方文檔進行學習,總結了幾點基本的
- RN開發需要的環境(mac,windows),參考官方文檔即可
- RN開發利用相關腳手架初始化項目,參考官方文檔即可
- RN打包需要用的簽名創建,及打包流程,參考官方文檔即可
- RN開發過程中用到的模擬器(我這里用的mumu模擬器)
- RN開發過程中用到的debugger工具(推薦使用react-native-debugger)
三、開發項目中用到的知識
學習的時候,我這里主要從以下幾方面進行學習的,列出的大部分是npm包名
- 設置app啟動圖片 react-native-splash-screen
- 頁面跳轉 react-navigation 起到路由的作用
- 數據存儲 @react-native-async-storage/async-storage
- 嵌入web react-native-webview
- 使用相機 react-native-camera
- 掃描二維碼 react-native-qrcode-scanner
- 文件上傳 react-native-document-picker react-native-doc-viewer
- 圖片上傳 react-native-image-picker
- 輪播圖 react-native-swiper
- 背景漸變 react-native-linear-gradient
- 頂部狀態欄 react-native 提供的組件 StatusBar
- toast提示 react-native-root-toast,會用到 react-native-root-siblings
- 手勢 react-native-gesture-handler
- 高德地圖 react-native-amap3d
四、ui庫
- react-native-element
- and-design-mobile-rn
- teaset
五、項目搭建
官網中提供的初始化模板
npx react-native init projectName --template react-native-template-typescript
六、根據需要安裝對應依賴
建議加上npm進行依賴搜索,或者去npm官網搜索,eg: react-native-camera npm
因為我這里項目初始化的rn版本為0.63.4,所以大部分依賴都是直接link之后就可以正常使用的。
七、開發和調試
靜態頁面的開發其實跟web端一樣,組件的合理利用就行。會涉及一些交互
1.物理返回鍵
const handle = ()=>{
? ? // 響應事件處理
}
// 訂閱
BackHandler.addEventListener('hardwareBackPress', handle);
// 移除
BackHandler.removeEventListener('hardwareBackPress', handle);
2.跨頁面通信
const handle = (params)=>{
? ? // 響應事件處理
}
// 訂閱
DeviceEventEmitter.addListener('eventKey', handle);
// 移除
DeviceEventEmitter.removeListener('eventKey', handle);
// 觸發
DeviceEventEmitter.emit('eventKey', params);
3. formData文件上傳
圖片或者文件上傳的時候,需要關閉debugger模式,不然會出現異常。?
原文鏈接:https://blog.csdn.net/juxiaoyu/article/details/109068115
相關推薦
- 2022-04-20 Android實現環信修改頭像和昵稱_Android
- 2022-03-07 Go語言參數傳遞是傳值還是傳引用_Golang
- 2022-03-29 C#算法之無重復字符的最長子串_C#教程
- 2022-10-31 Flask表單與表單驗證實現流程介紹_python
- 2022-11-10 Rust?use關鍵字妙用及模塊內容拆分方法_相關技巧
- 2022-05-17 Spring Cloud OpenFeign源碼解析
- 2022-01-02 適合初學者學習ajax表單提交的全部代碼
- 2022-04-12 git項目初次push提示error: failed to push some refs to ht
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支