簡體   English   中英

無法在React Native中導入其他組件

[英]Cannot import other components in React Native

最近,我開始學習如何使用React Native來構建移動應用程序,我從我的一位朋友那里聽說過它,我對此非常感興趣。 但是現在我遇到了錯誤,試圖從谷歌搜索解決方案,但是沒有一個可以解決我的問題。 我的問題是,當我將其他組件導入index.android.js時,它在移動屏幕上顯示錯誤=>

開發服務器返回的響應錯誤代碼:500

我的組件存儲在android => android / app / components / Bar.js文件夾中

我像這樣導入

從'./app/components/Bar'導入Bar;

index.android.js

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

import Bar from './app/components/Bar';
export default class ProfilePageApp extends Component {
  render() {
    return (
      <View style={styles.container}>

          <Bar />

      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#000',
  }
});

AppRegistry.registerComponent('ProfilePageApp', () => ProfilePageApp);

Bar.js

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


 class Bar extends Component {
  render() {
    return (
      <View style={styles.container}>

          <Text>Hello world</Text>

      </View>
    );
  }
}

export default Bar;

錯誤

在此處輸入圖片說明

我不確定RN在文件目錄約定方面的工作原理,但我認為它與React等其他人幾乎相同。 但是,當我閱讀您的代碼時,我認為您在import語句中缺少./android

您提到:

我的組件存儲在android => android / app / components / Bar.js文件夾中

但是您是這樣導入的:

import Bar from './app/components/Bar';

你有沒有嘗試過

import Bar from './android/app/components/Bar';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM