JavaScript中的“导出默认值”相当于Kotlin
我正在尝试使用Kotlin来构建我的React Native应用程序,并且正在将预先存在的JavaScript代码转换为它。 但是我不知道什么Kotlin相当于export default
。 是否还需要这样的代码?
我试图转换的JS类:
import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; export default class App extends React.Component { render() { return ( <View style={styles.container}> <Text>Open up App.js to start working on your app!</Text> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
目前,Kotlin / JS编译器按照模块工作,也就是为标记为模块(IDEA中的模块,Gradle中的项目或Maven模块中的模块)的一组Kotlin源文件生成单个JavaScript文件。 如果将moduleKind
编译器选项设置为commonjs
或umd
值,则Kotlin / JS会将此文件转换为CommonJS模块,并导入根包并导入所有相关模块。 没有提供的JavaScript代码的直接相当,所以你可能需要改变你的类被导入。
考虑你在Kotlin中声明这样的东西:
package my.pkg class App : React.Component() { // some code here }
在名为myModule
的模块中。 您可以通过以下代码将App
类导入到JavaScript中:
const App = require('myModule').my.pkg.App
有关Kotlin / JS如何与JavaScript交互的更多信息,请参阅文档:
- 在模块系统上 (包括CommonJS);
- 从JavaScript调用Kotlin ;
- 从Kotlin调用JavaScript 。