简体   繁体   English

为什么无法识别我的Index.js文件?

[英]Why won't my Index.js file be recognized?

I made a separate Index.js file to hold all the components that I want to export throughout the app I'm creating. 我制作了一个单独的Index.js文件,以容纳要在整个正在创建的应用程序中导出的所有组件。 I thought the file path I'm taking would be correct but instead... 我以为我要使用的文件路径是正确的,但是...

I'm getting an error that says: 我收到一条错误消息:

Could not resolve 无法解决
'/User/user/projects/Something/src/components/components' as a file '/ User / user / projects / Something / src / components / components'作为文件
nor as a folder (null) 也不作为文件夹(空)

目录路径的屏幕截图

How can I fix this? 我怎样才能解决这个问题?

Here's LoginForm.js : 这是LoginForm.js

import React, { Component } from 'react';
import { TouchableOpacity, StyleSheet, View, Text ,Button } from 'react-native';
import { UserInput } from './components';

class LoginForm extends Component {
  render() {
    return(
      <View style={styles.container}>
        <UserInput/>
          <TouchableOpacity style={styles.button}>
            <Button
              title="Press Me"
            />
          </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingHorizontal: 10
  },

  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10
  }
});

export default LoginForm;

Here's Index.js : 这是Index.js

export * from './UserInput';
export * from './LoginForm';

In the current working directory of LoginForm.js , there is no components folder. LoginForm.js的当前工作目录中,没有components文件夹。 From your script, you want UserInput component in the LoginForm component. 从脚本中,您需要LoginForm组件中的UserInput组件。

You can simply do it 你可以简单地做到

import { UserInput } from './UserInput';

Right use of index.js of components folder is when you want to import the selected component of folder's in other directory's component's 当您要在其他目录的组件目录中导入文件夹的选定组件时,正确使用components文件夹的index.js

update : 更新:

in the LoginForm component do LoginForm组件中

import  UserInput  from './UserInput'; //default  export

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM