无法从“ index.js”解析模块以获取自定义组件

Frakcool

我是新手,react-native目前正在尝试stack-navigator向我的应用中添加

// 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

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

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

但我不断收到此错误:

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给我带来了麻烦。

我的文件夹结构如下:

/
    index.js
    components/
        HomeScreen.js

我见过类似的问题,但它们都涉及第三方组件,react但不是由自己创建的。

我试图npm start --reset-cache按照这个答案的建议

如果有问题,我正在使用这些版本。

react-native-cli: 2.0.1
react-native: 0.61.2

如何解决这个错误?


在遵循@Vencovsky提出的解决方案之后(带有和不带有.js扩展名):

import HomeScreen from './components/HomeScreen.js'

我收到这个新错误:

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桥进行的:

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)
        }
    }
}
文科夫斯基

您需要将其导入为

import HomeScreen from './components/HomeScreen.js'

如果您仅将其导入为'./components',它将尝试获取./components.js./components/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)

第一行带有*-试图获取./components.js和其他扩展名
第二行带有*-试图获取./components/index.js和其他扩展名

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法从支持Bean获取自定义组件属性

无法解析模块`。/ index`

无法从Firestore获取自定义对象

无法读取自定义属性

React Native 错误:无法解析模块`./index`

React Native-使用我制作的自定义组件时出现错误(错误:undefined无法解析模块<MyModule>)

自定义模型无法从包含的模块中获取方法

无法从MapView获取自定义模型确实更改了状态功能

无法从其他应用获取自定义帐户的AuthToken

viewForAnnotation有时无法获取自定义MKAnnotation类并正确呈现它

无法通过Android Drive API获取自定义属性

无法获取自定义字体以加载到iOS应用中

无法使用JavaScript在客户端获取自定义标头

无法在 2checkout 响应中获取自定义参数

Sprite Kit-无法从对象获取自定义类数据

无法从 get_post_meta 获取自定义类型

无法在asp.net操作筛选器中获取自定义属性

Angular 4-无法在HttpErrorResponse中获取自定义HTTP标头

Django-无法获取自定义日志记录处理程序类

HPA无法从Prometheus适配器获取自定义指标

在select2中使用Ajax时无法获取自定义属性值

无法从jQuery click函数中的元素获取自定义属性

无法从自定义 node.js 模块导出函数

Webpack 无法解析自定义 JS 文件

无法读取自定义数据类型

闪亮的R无法读取自定义CSS文件

无法读取自定义反应隔间变量

修复错误:无法从''解析模块'./index.android'

react-navigation错误:无法解析模块“ ./vendor/index”