【问题标题】:Unable to resolve module from App.js - React Native无法从 App.js 解析模块 - React Native
【发布时间】:2020-11-25 18:57:39
【问题描述】:

我对反应很陌生,我知道这很愚蠢,但我真的看不到。

我收到以下错误:无法从“App.js”解析“./app/components/Apptext”

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

function AppText ({children})
{
return <Text style = {styles.text}>{children}</Text>
}



const styles = StyleSheet.create({

text:{
    color:"tomato",
   

    ...Platform.select({
        ios:{
            fontSize:20,
            fontFamily:"Avenir"
        },
        android:{
            fontSize:18,
            fontFamily:"Roboto"
        }
    })
},

})
export default AppText;

以上是我的 AppText 脚本

这是 app.js

import { StatusBar } from 'expo-status-bar';
import React, { Fragment } from 'react';
import {Dimensions ,SafeAreaView, StyleSheet,TouchableWithoutFeedback,Alert, Text, View ,Image, Button,Platform, BackHandler} from 'react-native';
import{ useDimensions,useDeviceOrientation } from '@react-native-community/hooks';
import WelcomeScreen from './app/screens/WelcomeScreen';
import AppText from './app/components/Apptext';


export default function App() {
  return (
<View style ={{flex:1,justifyContent:"center",alignItems:"center",}}>
      <AppText>I like react</AppText>
    </View>
    
    
    );
}

【问题讨论】:

标签: javascript reactjs react-native


【解决方案1】:

请检查 app.js 文件中的文件夹顺序/

【讨论】:

  • app.js 位于根文件夹中,而 Apptext 脚本位于名为 /app/components/Apptext 的子文件夹中
【解决方案2】:

不知道为什么,但是 react 对我如何实现这个脚本有问题,我把它改成下面的方式,它似乎工作正常。

import React from "react";
import { Text, StyleSheet, Platform } from "react-native";

function AppText({ children, style }) {
  return <Text style={[styles.text, style]}>{children}</Text>;
}

const styles = StyleSheet.create({
  text: {
    fontSize: 18,
    fontFamily: Platform.OS === "android" ? "Roboto" : "Avenir",
  },
});

export default AppText;

【讨论】:

    猜你喜欢
    • 2022-10-24
    • 2020-07-13
    • 1970-01-01
    • 2017-10-11
    • 2016-03-23
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2021-05-04
    相关资源
    最近更新 更多