【问题标题】:How can i export and import from one file to another in Ract Native如何在 React Native 中从一个文件导出和导入到另一个文件
【发布时间】:2015-10-28 05:14:56
【问题描述】:
var SummarizeAll=React.createClass({
render:function(){

    var self=this;
    var org = this.props.org;
    var root=this.props.root;    
    var name=rootSchema;
    var current=root;

     return (<ScrollView><View style={{marginLeft:5,marginRight:5}}>
          <Text>{current.displayName}</Text>
        {
          this.state.records.map(function(data,index){

            return (<GenreicApp/>)

          },this)
        }              
              </View>       
                </View>
              </View>
            </View>
        </View>
        </ScrollView>)
  }
})

这是在 index.ios.js 中

var GenericSummary=React.createClass({

render:function(){
   return (<View>
              <GenericDisplayView/>
              <SummarizeAll />
          </View>)                
 }
}
})

这是在 index2.ios.js 中

如何将 SummarizeAll 组件从 index.ios.js 文件中导出到 index2.ios.js 文件中的 GenericSumnmary 组件中

【问题讨论】:

    标签: ios ios-simulator react-native


    【解决方案1】:

    您需要使用 Javascript(或者更确切地说是 Node.js)模块:

    1. 将 SummarizeAll 类放在单独的 js 文件 (summarize_all.js) 中
    2. 在js文件末尾添加module.exports=SummarizeAll;
    3. 在要使用它的文件中使用 require 指令:var SummarizeAll = require('./summarize_all');
    4. 然后您可以类似地在 index.ios.js 或其他地方要求 SummarizeAll 组件。将其放在单独的文件/子目录中使其独立且可重复使用。

    为了清晰和更好的模块化,您还可以将 .js 文件放在某个子目录中,在这种情况下,require 应该包含您的文件的相对路径,包括子目录。

    【讨论】:

    • 谢谢 Jarek 先生,它正在工作
    • 我可以用同样的现象将函数从一个js导入另一个js文件吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多