如何修复Expo Print.printToFileAsync(options)undefined不是对象错误

时间:2019-06-25 03:07:30

标签: react-native expo

我正在尝试使用Expo的Print.printToFileAsync(选项,但我一直在获取[未处理的承诺拒绝:TypeError:未定义不是对象(正在评估'_expoPrint.Print.printToFileAsync')”)。

我在网上寻找了很多解决方案,但是找不到解决方案。我开始使用React Native库,但是当我搜索时,发现我只能使用Expo的库,因此我切换到Print.printToFileAsync()。

App.js

import React, {Component} from 'react';
import { StyleSheet, Text, View, TouchableHighlight } from 'react-native';
import { Print } from 'expo-print';

export default class App extends Component {

  async createPDF() {

    let filePath = await Print.printToFileAsync({
      html: "<h1>PDF TEST</h1>",
      width : 612,
      height : 792,
      base64 : false
    });

    alert('PDF Generated', filePath.uri);
  }

  render() {
    return(
      <View>
        <TouchableHighlight onPress={this.createPDF} style={styles.Main}>
          <Text>Create PDF</Text>
        </TouchableHighlight>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  Main : { marginTop : 100 }
});

Package.JSON

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "expo": "^33.0.0",
    "expo-print": "^5.0.1",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
    "react-native-html-to-pdf": "^0.7.0",
    "react-native-share": "^1.2.1",
    "react-native-web": "^0.11.4"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.1.1"
  },
  "private": true
}

我的最终目标是在Expo项目中使用HTML制作PDF文件。

2 个答案:

答案 0 :(得分:0)

Expo sdk33需要安装模块并直接运行。

expo install expo-print

模块化导入

对于SDK 33,我们不建议从expo包中导入大多数模块。这意味着在将来的版本中,您将无法编写例如,import { FileSystem } from 'expo';。相反,您需要为您使用的每个模块安装单独的软件包,然后从它们中导入。

您可以使用新的expo install命令来安装模块;此命令是npm install/yarn add的包装,可自动安装与您的SDK版本兼容的模块版本。例如,对于FileSystem模块,您将运行expo install expo-file-system,然后使用import * as FileSystem from 'expo-file-system';。此更改为摇树和较小的JavaScript包铺平了道路。它还使在托管工作流和裸露工作流之间的转换更加简单。

expo软件包中的导入将继续在SDK 33中运行,但会在控制台中生成警告,您需要从各个软件包中导入以使警告消失。为了简化此更改,我们提供了codemod,它将自动为您更新所有导入。

主页上有SDK33

的描述

答案 1 :(得分:0)

请使用- 从'expo-print'导入*作为Print;

而不是- 从“ expo-print”导入{Print};