我对react-native很陌生,我正在尝试使用Realm。我已经完成了 react-native 链接 Realm 和 rnpm 链接 Realm 。但是当我尝试导入 Realm 时,出现错误未知执行上下文,这是我的 index.android.js
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
} from 'react-native';
import { TabViewAnimated, TabBar } from 'react-native-tab-view';
import Today from './app/Today'
import Realm from 'realm'
import _ from 'lodash'
const styles = StyleSheet.create({
container: {
flex: 1,
},
page: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
export default class ExpenseManagerProject extends Component {
state = {
index: 0,
routes: [
{ key: '1', title: 'Today' },
{ key: '2', title: 'Category' },
{ key: '3', title: 'Date' },
],
};
_handleChangeTab = (index) => {
this.setState({ index });
};
_renderFooter = (props) => {
return <TabBar {...props} />;
};
_renderScene = ({ route }) => {
switch (route.key) {
case '1':
return <Today/>;
case '2':
return <View style={[ styles.page, { backgroundColor: '#673ab7' } ]} />;
default:
return null;
}
};
render() {
return (
<TabViewAnimated
style={styles.container}
navigationState={this.state}
renderScene={this._renderScene}
renderFooter={this._renderFooter}
onRequestChangeTab={this._handleChangeTab}
/>
);
}
}
AppRegistry.registerComponent('ExpenseManagerProject', () => ExpenseManagerProject);
最佳答案
Add the following lines to android/settings.gradle:
gradle include ':realm' project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
Add the compile line to the dependencies in android/app/build.gradle:
gradle dependencies { compile project(':realm') }
Add the import and link the package in android/app/src/main/java/com/[your-application-name]/MainApplication.java:
import io.realm.react.RealmReactPackage; // add this import
public class MainApplication extends Application implements ReactApplication {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RealmReactPackage() // add this line
);
}
}
对我来说,这只是最后一步,将行添加到 MainApplication.java 中,这是我必须做的,因为其他内容已经存在。因此,请务必检查提到的所有文件。
如果还是不行,新建一个项目,添加Realm(包括上面的步骤),添加之前写的所有文件。
关于android - 在 react native 中导入 Realm 时未知的执行上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42557984/