【发布时间】:2019-01-02 15:07:13
【问题描述】:
我正在尝试在 react-native 中使用 npm oxford-dictionary 包,我安装了 “npm install oxford-dictioary”并在终端上将此文件作为“node app.js”运行并且工作正常,但是,当我尝试在 react native 中使用它不起作用时,谁能帮我解决问题。是否可以仅通过 react-native 使用此 api,或者我必须使用 nodejs 构建后端?
app.js
var Dictionary = require("oxford-dictionary");
var config = {
app_id : "-------",
app_key : "--------",
source_lang : "en"
};
var dict = new Dictionary(config);
var props = {
word: "stupendous",
};
var oxfordApi = {
getRovers(){
var lookup = dict.synonyms(props);
return fetch(lookup).then((res) => res.json());
}
}
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
//import oxfordApi from './oxfordApi';
import globals from 'node-libs-react-native/globals';
var Dictionary = require("oxford-dictionary");
var config = {
app_id : "-------",
app_key : "--------------------------",
source_lang : "en"
};
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
rovers: []
}
}
componentWillMount() {
var dict = new Dictionary(config);
var lookup = dict.find("awesome");
lookup.then((res) => {
this.setState({
rovers: res.rovers
})
});
}
render() {
console.log("Rovers: ", this.state.rovers);
return (
<View style={styles.container}>
<Text>Hello wordl</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
当我运行“run npm ios”时的结果
“node_modules/oxford-dictionary/index.js”中的包试图导入 Node 标准库模块“https”。它失败了,因为 React Native 不包含 Node 标准库。阅读更多https://docs.expo.io/versions/latest/introduction/faq.html#can-i-use-nodejs-packages-with-expo
【问题讨论】:
-
我重新编写了包、方法并将其发布到 npm。 react-native-oxford-dictionary
标签: node.js react-native