Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React Native for cross platform应用程序中使用index.js而不是(index.ios.js,index.android.js)?_Javascript_Android_Ios_React Native_Cross Platform - Fatal编程技术网

Javascript 如何在React Native for cross platform应用程序中使用index.js而不是(index.ios.js,index.android.js)?

Javascript 如何在React Native for cross platform应用程序中使用index.js而不是(index.ios.js,index.android.js)?,javascript,android,ios,react-native,cross-platform,Javascript,Android,Ios,React Native,Cross Platform,谢谢你从现在开始的回答 我是《React Native》的新手,我想制作一个跨平台的应用程序,所以我创建了index.js: import React from 'react'; import { Component, View, Text, } from 'react-native'; class ReactApp extends Component { render() { return ( <View>

谢谢你从现在开始的回答

我是《React Native》的新手,我想制作一个跨平台的应用程序,所以我创建了index.js:

import React from 'react';
import {
    Component,
    View,
    Text,
} from 'react-native';

class ReactApp extends Component {

    render() {
        return (
            <View><Text>Hello world</Text></View>
        );
    }
}

module.exports = ReactApp;
我想在这之后它应该会起作用,但我明白了 错误:


这是一个倒退
index.ios.js
index.android.js
始终是默认
react native init
项目中的独立入口点。如果您想让它们通过
index.js
运行相同的代码库,您应该将其设置为
index.ios.js
index.android.js
导入
index.js
并注册
index.js
中定义的相同基本组件

例如,您可以查看在这个()中是如何完成的

如果将
index.js
放在根文件夹中,当您不直接引用它时,它将与
index.android.js
index.ios.js
冲突。因此,您需要在导入中指向确切的路径。请参阅下面的代码

index.ios.jsindex.android.js(内容相同)

index.js

// Note: I noticed that you imported Component from the wrong place. That might also be contributing to your issue so I fixed it here.
import React, { Component } from 'react';
import {
    View,
    Text,
} from 'react-native';

// Unless you are exporting multiple things from a single file, you should just use this.
// It's more idiomatic than using module.exports = ReactApp;
export default class ReactApp extends Component {
    render() {
        return (
            <View><Text>Hello world</Text></View>
        );
    }
}
//注意:我注意到您从错误的位置导入了组件。这也可能导致您的问题,所以我在这里修复了它。
从“React”导入React,{Component};
进口{
看法
文本,
}从“反应本机”;
//除非您要从一个文件中导出多个内容,否则您应该只使用这个。
//它比使用module.exports=ReactApp更惯用;
导出默认类ReactApp扩展组件{
render(){
返回(
你好,世界
);
}
}

在React v0.49之后,您不需要
index.ios.js
index.android.js
。您只需要
index.js

import {AppRegistry} from 'react-native';
import App from './app/App';

AppRegistry.registerComponent('appMobile', () => App);
(用应用程序的名称替换appMobile

资料来源:()

从现在开始,新项目只有一个入口点(index.js)


在iOS AppDelegate.m中更改此选项

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];


请重命名索引文件并重试您的图像不工作。另外,你没有说明你想通过这样做实现什么。@MichaelCheng image正在工作,我想因为我也试过手机,我想做一个跨平台的app@ravi.p怎么样?为什么?用这个替换index.js中的第一行,并尝试从“React”导入React,{Component};我复制并粘贴了您的代码,但得到了相同的错误:-/,@davutdev请立即尝试编辑。因为我很快就把答案拼凑起来了,所以我错过了答案。在所有三个文件中,您仍然需要它。此外,还意识到如果将
index.js
与以
index.something开始的其他文件放在同一目录中,那么速记
/index
导入将产生命名冲突。如果您使用了另一个文件名或将其放置在其他位置,则不会出现问题。我在回答中添加了注释来解释这一点。我刚才还在一个全新的项目中测试了这个,所以如果仍然有任何错误,它一定是来自其他方面。这是我一直在寻找的,因为我使用过其他版本(旧版本),我看到了2个不同的文件,但从0.49开始,创建应用程序后将得到一个文件。如果你想升级你的RN版本检查(
react native git upgrade
应该像charme一样工作)是否仍然可以使用2个文件和新的react native?我不认为这个问题解释了什么:
import {AppRegistry} from 'react-native';
import App from './app/App';

AppRegistry.registerComponent('appMobile', () => App);
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];