【发布时间】:2019-07-04 23:25:07
【问题描述】:
我为 android 创建了一个原生模块(使用 kotlin 语言)
class TestModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return "TestModule"
}
@ReactMethod
fun testing(): String{
return "my modules"
}
}
然后我在 ModulesPackages 中注册了这个模块,并按照https://facebook.github.io/react-native/docs/native-modules-android.html 的说明将其添加到 MainApplication.tk
在 App.js 中
import {NativeModules} from 'react-native'
const TestingModule = NativeModules.TestModule;
type Props = {};
export default class App extends Component<Props> {
render() {
alert(TestingModule.testing());
return (
<View style={styles.container}>
<Text style={styles.instructions}> test</Text>
</View>
);
}
}
运行此程序后,我希望收到带有文本“我的模块”的警报,但我收到了带有文本“未定义”的警报
我不知道我做错了什么。我运行 react-native run-android 时没有看到任何错误或抱怨。
任何建议我做错了什么?
【问题讨论】:
标签: react-native react-native-android