javascript - 在 React Native 中加载组件时出错

标签 javascript reactjs react-native

我在尝试加载我的组件时遇到错误:

Failed to load bundle(http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false) with error:(Unable to resolve module components/Items from /Users/vemundeldegard/AwesomeProject/app/screens/Home.js: Module components/Items does not exist in the Haste module map

This might be related to https://github.com/facebook/react-native/issues/4968 To resolve try the following: 1. Clear watchman watches: watchman watch-del-all. 2. Delete the node_modules folder: rm -rf node_modules && npm install. 3. Reset Metro Bundler cache: rm -rf /tmp/metro-bundler-cache-* or npm start -- --reset-cache. 4. Remove haste cache: rm -rf /tmp/haste-map-react-native-packager-*. (null))

尝试在名为 screens/home.js 的屏幕中加载我的组件,然后将其加载到我的 app.js 中。

将此添加到/screens/home.js 文件的顶部:

import Items from 'components/Items';

然后像这样在渲染中调用它 <Items />

components/items.js 看起来像这样:

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View
} from 'react-native';

const Items = () => {
  return (
    <View>This is now an item</View>
  )
}
export default Items;

为什么我无法加载组件?尝试按照错误提示进行操作。

enter image description here

最佳答案

尝试import Items from '../components/items',实际上你的文件被命名为items(没有大写i)而不是Items.

关于javascript - 在 React Native 中加载组件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52282980/

相关文章:

javascript - Three.js 中 Json 格式几何的线框

javascript - jQuery AJAX 调用 MVC Controller 操作永远不会返回

javascript - 将数据传递给父级 - Recipebook React

javascript - 没有状态和 Prop 的 React App

javascript - 如何将 ScrollView 与嵌套的 FlatList 一起使用?

react-native - 在 react 导航中动态更改 DrawerNavigator 的 drawerPosition 配置

javascript - React native - 如何过滤数组异步(async/await)

javascript - 如何在 onPress 的 react-native-maps 中获取标记的键或坐标?

javascript - 遍历 JSON 时 react 极慢

javascript - 如何从javascript中的数组中获取文本并将其附加到文本区域?