简体   繁体   English

在 React Native 中加载组件时出错

[英]Getting an error when loading component in React Native

I am getting an error when trying to load my component:尝试加载我的组件时出现错误:

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无法加载包( http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false ),错误:(无法从/Users/vemundeldegard/AwesomeProject/app/screens/Home.js解析模块components/Items /Users/vemundeldegard/AwesomeProject/app/screens/Home.js : Haste 模块映射中不存在模块components/Items

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 .这可能与https://github.com/facebook/react-native/issues/4968要解决尝试以下操作: 1. 清除watchman watch-del-all : watchman watch-del-all 2. Delete the node_modules folder: rm -rf node_modules && npm install . 2. 删除node_modules文件夹: rm -rf node_modules && npm install 3. Reset Metro Bundler cache: rm -rf /tmp/metro-bundler-cache-* or npm start -- --reset-cache . 3. 重置 Metro Bundler 缓存: rm -rf /tmp/metro-bundler-cache-*npm start -- --reset-cache 4. Remove haste cache: rm -rf /tmp/haste-map-react-native-packager-* . 4. 移除加速缓存: rm -rf /tmp/haste-map-react-native-packager-* (null)) (空值))

Trying to load my component in screen which is called screens/home.js, which is then loaded in my app.js.尝试在名为screens/home.js 的屏幕中加载我的组件,然后将其加载到我的app.js 中。

Added this to the top of the /screens/home.js file:将此添加到 /screens/home.js 文件的顶部:

import Items from 'components/Items';

And calling it in the render like this <Items />并像这样在渲染中调用它<Items />

components/items.js looks like this: 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;

Why can't I load a component?为什么我无法加载组件? Tried doing what the error says.尝试做错误所说的。

在此处输入图片说明

尝试import Items from '../components/items' ,实际上您的文件名为items (不带大写 i)而不是Items

If the file location is right as you mentioned, import Items from '../components/items';如果文件位置与您提到的一样正确,请import Items from '../components/items'; should be right.应该是对的。 But the important thing is after you add new files such as js or image files, you need to restart the node modules.但重要的是在你添加了新的文件比如 js 或图像文件后,你需要重新启动节点模块。

Try:尝试:

import Items from '../components/Items';从“../components/Items”导入项目;

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

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