在react-native中导入领域时未知的执行上下文 [英] unknown execution context when import realm in react-native
问题描述
我对本机反应真的很陌生,我正在尝试使用Realm.我已经完成了 react-native链接领域和 rnpm链接领域.但是当我尝试导入Realm时出现错误未知执行上下文,这是我的 index.android.js
Im really new to react-native and Im trying to use Realm. I already done the react-native link realm and rnpm link realm. But i get the error unknown execution context when I try to import Realm, heres my 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);
推荐答案
根据文档:
将以下行添加到android/settings.gradle:
Add the following lines to android/settings.gradle:
gradle include ':realm' project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
将编译行添加到android/app/build.gradle中的依赖项:
Add the compile line to the dependencies in android/app/build.gradle:
gradle dependencies { compile project(':realm') }
添加导入并将包链接到android/app/src/main/java/com/[您的应用程序名称]/MainApplication.java:
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中,因为其他内容已经存在.因此,请确保您检查了所有提到的文件.
For me, it was only the last step, adding the lines to MainApplication.java, that I had to do, since the other stuff was already present. So make sure you check all the files mentioned.
如果仍然无法运行,请创建一个新项目,添加Realm(包括上述步骤),然后添加您之前编写的所有文件.
这篇关于在react-native中导入领域时未知的执行上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!