如何从 React Native ios 项目中的组件文件夹访问 js 文件

How to access js files from components folder in React Native ios project

我无法访问 React Native 项目中的组件文件夹 IOS。

我收到以下错误:

Unable to resolve module ./Login from ....../ReactNative/ReactNativeProject/components/App.js: Unable to find this module in its module map or any of the node_modules directories under ......./ReactNative/ReactNativeProject/components/Login.j and its parent directories.

我参考了以下 link: http://caroaguilar.com/post/react-native-navigation-tutorial/

index.ios.js (ReactNativeProject/index.ios.js)

"use strict";

import React, { AppRegistry } from 'react-native';
import App from './components/App';

AppRegistry.registerComponent('ReactNativeProject', () => App);

App.js (ReactNativeProject/components/App.js)

  'use strict'

    import React, {Component} from 'react';
    import {
        AppRegistry,
        StyleSheet,
        NavigatorIOS,
    } from 'react-native';
    var Login = require('./Login');

    class App extends Component {
        render() {
            return (
              <NavigatorIOS
                style={styles.navigationContainer}
                initialRoute={{
                title: "Login Page",
                component: Login,
              }} />
          );
        }
    }

    var styles = StyleSheet.create({
        navigationContainer: {
            flex: 1
        }
    });

    export default App;

Login.js (ReactNativeProject/components/Login.js)

"use strict";
    import React, {Component} from 'react';
    import {
        StyleSheet,
        Text,
        TextInput
    } from 'react-native';
    import Button from 'react-native-button';
    import styles from './login';


    class Login extends Component {

        constructor(props) {
            super(props);
            this.state = {
              username: "",
              password: "",

            };
        }

        render() {
            return (

              <View style={styles.container}>
                  <View style={styles.textContainer}>
                      <TextInput
                          style={styles.inputUsername}
                          placeholder="Enter email ID"
                          value={this.state.username}
                          clearButtonMode = 'while-editing'/>
                      <TextInput
                          style={styles.inputPassword}
                          placeholder="Enter Password"
                          value={this.state.password}
                          password={true}
                          secureTextEntry={true}
                          clearButtonMode = 'while-editing' />

                     <Button style={styles.login}
                             styleDisabled={{color: 'red'}}>
                             Login
                      </Button>
                  </View>
              </View>
            );
        }

    module.exports = Login;

到目前为止我已经尝试过并找到了解决方案。

我在App.js中犯了一个错误:

我已经替换了var Login = require('./Login');

来自

import Login from './Login';

components文件夹下的js文件也发生了如下变化,除了App.js

Login.js 的变化:

class Login extends Component {
   }

改为

class Login extends React.Component {
}

好吧,我这样做是为了从 1 根目录 和整个项目结构的 根目录 导入 js 文件.

我有以下目录结构。

App.js 文件位于 root directory

Splash.js 文件位于 MyApp -> Splash -> Splash.js

Home.js 文件位于 MyApp -> Home -> Home.js

TextViewComponent 文件位于 MyApp -> CustomComponents -> TextViewComponent.js

我如何访问所有文件中的所有文件。

希望这对你也有帮助。

完成。