首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法为自定义组件从“index.js”解析模块

无法为自定义组件从“index.js”解析模块
EN

Stack Overflow用户
提问于 2019-10-22 17:14:51
回答 2查看 3.4K关注 0票数 2

我对react-native有点陌生,目前正在尝试在我的应用程序中添加一个stack-navigator

代码语言:javascript
复制
// import React from 'react';
import {Text, View, AppRegistry} from 'react-native';
import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';

// class HomeScreen extends React.Component {
//   static navigationOptions = {
//     title: 'Welcome',
//   };
//   render() {
//     return (
//       <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
//         <Text>Home Screen</Text>
//       </View>
//     );
//   }
// }

const MainNavigator = createStackNavigator({
  Home: {screen: HomeScreen}
}, {
  initialRouteName: 'Home'
})

const NavigationApp = createAppContainer(MainNavigator);

AppRegistry.registerComponent('ReactNativeApp', () => NavigationApp)

export default NavigationApp

如果我取消对上面示例中注释的代码的注释,那么一切都正常,现在我尝试将HomeScreen组件移动到它自己的文件中,所以我尝试了如下:

HomeScreen.js

代码语言:javascript
复制
class HomeScreen extends React.Component {
  static navigationOptions = {
    title: 'Welcome',
  };
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Home Screen</Text>
      </View>
    );
  }
}

index.js

代码语言:javascript
复制
import HomeScreen from './components'
import {AppRegistry} from 'react-native';
import {createAppContainer} from 'react-navigation';
import {createStackNavigator} from 'react-navigation-stack';

const MainNavigator = createStackNavigator({
  Home: {screen: HomeScreen}
}, {
  initialRouteName: 'Home'
})

const NavigationApp = createAppContainer(MainNavigator);

AppRegistry.registerComponent('ReactNativeApp', () => NavigationApp)

export default NavigationApp

但是我不断地发现这个错误:

代码语言:javascript
复制
error: bundling failed: Error: Unable to resolve module `./components` from `index.js`: 

None of these files exist:
  * components(.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
  * components/index(.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
    at ModuleResolver.resolveDependency (/Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:163:15)
    at ResolutionRequest.resolveDependency (/Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:52:18)
    at DependencyGraph.resolveDependency (/Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/node-haste/DependencyGraph.js:282:16)
    at Object.resolve (/Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/lib/transformHelpers.js:267:42)
    at /Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:426:31
    at Array.map (<anonymous>)
    at resolveDependencies (/Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:423:18)
    at /Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:275:33
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/myuser/git/POC/ReactNativeApp/ReactNativeApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:87:24)

在过去,我能够像我一样导入它们,但是react-navigation给我带来了问题。

我拥有的文件夹结构如下:

代码语言:javascript
复制
/
    index.js
    components/
        HomeScreen.js

我也看到过类似的问题,但所有这些问题都涉及react的第三方组件,而不是自己制造的组件。

我已经按照npm start --reset-cache中的建议尝试了这个答案

如果有关系的话我就用这些版本。

代码语言:javascript
复制
react-native-cli: 2.0.1
react-native: 0.61.2

如何修复此错误?

在遵循@Vencovsky提出的解决方案之后(有或没有.js扩展):

代码语言:javascript
复制
import HomeScreen from './components/HomeScreen.js'

我发现了一个新的错误:

代码语言:javascript
复制
The component for route 'Home' must be a React component. For example:

import MyScreen from './MyScreen';
...
Home: MyScreen,
}

You can also use a navigator:

import MyNavigator from './MyNavigator';
...
Home: MyNavigator,
}

我调用这个React应用程序的方式是这样通过一个Swift桥:

代码语言:javascript
复制
import Foundation
import UIKit
import React

class ButtonController: UIViewController  {
    @IBOutlet weak var button: UIButton!

    @IBAction func buttonClicked(_ sender: Any) {
        if let jsBundleLocation = URL(string: "http://X.X.X.X:8081/index.bundle?platform=ios") {
            //The data is used as initialProperties to React Native App.
            let data:NSDictionary = ["onNavigationStateChange": "{handleNavigationChange}",
                                     "uriPrefix":"/app"]; //We can use this parameter to pass the data to the React native App from the Native App.
            //The RCTRootView is a native view used to host React-managed views within the app. Can be used just like any ordinary UIView.
            let rootView = RCTRootView(
                bundleURL: jsBundleLocation,
                moduleName: "ReactNativeApp",
                initialProperties: data as [NSObject : AnyObject],
                launchOptions: nil)
            let viewController = UIViewController()
            viewController.view = rootView
            self.present(viewController, animated: true, completion: nil)
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-22 17:26:24

您需要将它作为

代码语言:javascript
复制
import HomeScreen from './components/HomeScreen.js'

如果您只将其导入为'./components',它将尝试获取./components.js./components/index.js (以及其他扩展),但您没有这些。

如果你看一下错误,它说它试图获取这些文件,但没有找到。

这些文件都不存在:* components(.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx) * components/index(.native|.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)

*的第一行--尝试获取./components.js和其他扩展

*的第二行--尝试获取./components/index.js和其他扩展

票数 4
EN

Stack Overflow用户

发布于 2019-10-22 18:34:46

我在@Vencovsky回答之后解决了第二个问题:

代码语言:javascript
复制
export class HomeScreen extends React.Component

至:

代码语言:javascript
复制
export default class HomeScreen extends React.Component

在阅读了这个回答/评论 on GitHub之后。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58509318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档