升级Expo SDK 49后Expo Print功能失效,报TypeError错误
Expo SDK49升级后Print功能报错解决
问题原因
Expo SDK49对expo-print库的API做了变更,Orientation枚举不再挂载在Print对象下,导致原代码中Print.Orientation访问时返回undefined,触发Cannot read property 'Orientation' of undefined错误。
解决方案
方法1:导入独立的Orientation枚举
修改导入语句,单独引入Orientation:
// 原导入 import * as Print from 'expo-print'; // 修改后 import * as Print from 'expo-print'; import { Orientation } from 'expo-print';
然后替换代码中的枚举调用:
const printAbac = async () => { await Print.printAsync({ html: isoSheet, printerUrl: selectedPrinter?.url, orientation: Orientation.portrait // 替换此处 }); };
方法2:直接使用字符串值
Orientation枚举对应的实际值为字符串'portrait'和'landscape',可以直接传入字符串替代枚举:
const printAbac = async () => { await Print.printAsync({ html: isoSheet, printerUrl: selectedPrinter?.url, orientation: 'portrait' // 直接使用字符串 }); };
其他函数验证
printToFile和selectPrinter的API在SDK49中未发生变更,无需修改,解决Orientation的问题后即可正常运行。
内容的提问来源于stack exchange,提问作者alpha go
相关产品推荐
相关产品推荐

