反应本机项目结构问题

时间:2020-05-19 23:07:47

标签: reactjs react-native expo file-structure

我刚刚开始使用react native和expo,并且已经一遍又一遍地重组文件夹,因为我遇到了多个错误,其中js文件找不到位于./asses/navigation/the_image下的图像。 .png。我唯一可以使用的方法是将所有js文件放在App.js中,并将App.js放在“ ./”下,并将图片放在同一位置。

这是错误的样子:error

这就是我的项目结构:project structure

构造项目的正确方法是什么?如何获取诸如profile.js之类的文件以查找资产中的图像?

messages.js的代码:

import 'react-native-gesture-handler';
import React, { useState } from 'react';
import { Image, StyleSheet, TouchableOpacity, View, TextInput } from 'react-native';

export default function messages({ navigation })
{
  const [filtertext, setfilter] = useState('');
  return(
    <View style={styles.home_container}>
      <View style={styles.home_background}>
        <TextInput
          style= {styles.filter} 
          placeholder= "Search"
          placeholderTextColor= "#96a7af"
          onChangeText={filtertext => setfilter(filtertext)}
          defaultValue={filtertext}
        />

        <Image source= {require ('./assets/navigation/3_Elements_Circled_Navigation_Message_On.png')}/> 

        <TouchableOpacity onPress={() =>navigation.navigate('Home')} style={styles.home_button}>        
          <Image source= {require ('./assets/buttons/new_message_icon.png')} style={styles.new_msg_buton}/> 
        </TouchableOpacity>

        <TouchableOpacity onPress={() =>navigation.navigate('Profile')} style={styles.profile_button}>        
          <Image source= {require ('./assets/buttons/profile_icon.png')}/> 
        </TouchableOpacity>

      </View>
    </View>
  );
}

const styles = StyleSheet.create(
{
  new_msg_buton:
  {
    position:'absolute',
    height: 60,
    width: 60,
    top: -696,
    left: 129,
  },

  filter:
  {
    position: 'absolute',
    height: 54,
    width: 275,
    top: -660,
    left: 19,
    paddingLeft: 20,
    borderColor: 'black',
    borderRadius: 23,
    borderTopWidth: 3,
    borderBottomWidth: 3,
    borderLeftWidth: 3,
    borderRightWidth: 3,
  }
})

1 个答案:

答案 0 :(得分:1)

使用./指向文件所在的当前目录

在文件夹结构中上一层时,使用../dir_onelevel_above指的是dir_onelevel_above

话虽如此,您应该需要../assets/navigation/icon.png才能从messages.js访问图像

相关问题