我正在尝试构建条形码扫描仪网络应用程序。我在线上找到了一些代码示例,但是当我运行yarn start-web
时,出现以下错误:
无法编译。
./ node_modules / expo / build / Notifications / Notifications.js 尝试导入错误:“ DeviceEventEmitter”未从“ react-native”导出。
package.json
{
"name": "BarcodeExpo",
"version": "0.1.0",
"private": true,
"devDependencies": {
"jest-expo": "~27.0.0",
"react-native-scripts": "1.14.0",
"react-test-renderer": "16.3.1"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "jest",
"start-web": "react-scripts start",
"build-web": "react-scripts build"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "32.0.0",
"query-string": "^6.2.0",
"react": "16.7.0",
"react-art": "^16.7.0",
"react-dom": "^16.7.0",
"react-native": "0.57.8",
"react-native-web": "^0.9.13",
"react-router-dom": "^4.3.1",
"react-router-native": "^4.3.0",
"react-scripts": "^2.1.3"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
App.js
'use strict';
import React, { Component } from 'react';
import { Text, View, StyleSheet, Alert } from 'react-native';
import { Constants, BarCodeScanner, Permissions } from 'expo';
export default class App extends Component {
state = {
hasCameraPermission: null
};
componentDidMount() {
this._requestCameraPermission();
}
_requestCameraPermission = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({
hasCameraPermission: status === 'granted',
});
};
_handleBarCodeRead = data => {
Alert.alert(
'Scan successful!',
JSON.stringify(data)
);
};
render() {
return (
<View style={styles.container}>
{this.state.hasCameraPermission === null ?
<Text>Requesting for camera permission</Text> :
this.state.hasCameraPermission === false ?
<Text>Camera permission is not granted</Text> :
<BarCodeScanner
onBarCodeRead={this._handleBarCodeRead}
style={{ height: 200, width: 200 }}
/>
}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
}
});
是否可能是expo模块中的错误?
修改
我创建了一个仓库(https://github.com/albertski/barcode),该仓库基本上具有默认的Expo项目,但是一旦我将import { Constants, BarCodeScanner, Permissions } from 'expo';
添加到App.js文件并运行yarn start-web
,我就会收到错误消息。
答案 0 :(得分:0)
我认为这里的问题是我正在尝试在网络应用上使用BarCodeScanner
,但这仅适用于IOS / Android。