【问题标题】:How to access js files from components folder in React Native ios project如何从 React Native ios 项目中的组件文件夹访问 js 文件
【发布时间】:2017-01-30 14:46:48
【问题描述】:

我无法访问 React Native Project IOS 中的 components 文件夹。

我收到以下错误:

无法解析模块 ./Login from ....../ReactNative/ReactNativeProject/components/App.js:找不到 此模块在其模块映射或任何 node_modules 目录中 在 ......./ReactNative/ReactNativeProject/components/Login.j 及其 父目录。

我参考了以下链接: 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;

【问题讨论】:

    标签: javascript ios node.js react-native react-native-android


    【解决方案1】:

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

    我在 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 {
    }
    

    【讨论】:

      【解决方案2】:

      我这样做是从整个项目结构的1个后根目录根目录导入js文件。

      我有以下目录结构。

      App.js 文件位于root directory

      Splash.js 文件位于MyApp -&gt; Splash -&gt; Splash.js

      Home.js 文件位于MyApp -&gt; Home -&gt; Home.js

      TextViewComponent 文件位于MyApp -&gt; CustomComponents -&gt; TextViewComponent.js

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

      希望这对你也有帮助。

      完成。

      【讨论】:

        猜你喜欢
        • 2018-11-26
        • 1970-01-01
        • 2021-07-22
        • 2019-03-19
        • 1970-01-01
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多