[英]Unable to resolve module ./SignIn In React Native app
嗨,我在 React Native 中收到一个错误,说它无法解析模块。 这是说某个文件夹不存在,但路径是准确的。 它告诉我 Directory /Users/Administrator/test/App/SignIn.js
不存在。我的目录看起来像这样
登录.js
import React from 'react'
import {
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
} from 'react-native'
import axios from 'axios'
import AsyncStorage from '@react-native-community/async-storage'
class SignIn extends React.Component {
state = {
email: '',
password: '',
loading: false,
}
onChangeHandle(state, value) {
this.setState({
[state]: value,
})
}
dologin() {
const { email, password } = this.state
if (email && password) {
const req = {
email: email,
password: password,
}
this.setState({
loading: true,
})
axios.post('https://reqres.in/api/login', req).then(
(res) => {
this.setState({
loading: false,
})
AsyncStorage.setItem('token', res.data.token).then((res) => {
this.props.navigation.navigate('App')
alert('Login Successful')
})
},
(err) => {
this.setState({
loading: false,
})
alert('User Name Password is wrong')
}
)
} else {
alert('Enter Email and password')
}
}
render() {
const { email, password, loading } = this.state
return (
<View style={styles.signin}>
<Text style={styles.string}>Hello There!</Text>
<Text style={styles.string}>Welcome Back</Text>
<Text style={styles.Text}>EMAIL</Text>
<TextInput
style={styles.textinput}
placeholder="Please Enter Your Email"
underlineColorAndroid={'transparent'}
keyboardType="email-address"
value={email}
onChangeText={(value) => this.setState({ email: value })}
disabled={loading}
/>
<Text style={styles.Text}>PASSWORD</Text>
<TextInput
style={styles.textinput}
placeholder="Please Enter Your Password"
secureTextEntry={true}
underlineColorAndroid={'transparent'}
value={password}
onChangeText={(value) => this.setState({ password: value })}
/>
<TouchableOpacity
style={styles.button}
activeOpacity={0.8}
style={{
...styles.signinBtn,
backgroundColor: loading ? '#ddd' : 'blue',
}}
onPress={() => this.dologin()}
disabled={loading}
>
<Text style={styles.btntext}>
{loading ? 'Loading...' : 'SignIn'}
Sign In
</Text>
</TouchableOpacity>
</View>
)
}
}
export default SignIn
const styles = StyleSheet.create({
signin: {
alignSelf: 'stretch',
},
textinput: {
alignSelf: 'stretch',
height: 40,
marginBottom: 30,
color: '#1D1105',
borderBottomColor: '#f8f8f8',
borderBottomWidth: 1,
},
button: {
alignSelf: 'stretch',
alignItems: 'center',
padding: 20,
backgroundColor: '#6069f2',
marginTop: 30,
},
btntext: {
color: '#fff',
fontWeight: 'bold',
},
string: {
alignContent: 'center',
justifyContent: 'center',
textAlign: 'center',
},
})
路由.js
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import SignIn from './App/SignIn'
import Dashboard from './App/DashBoard'
import AuthLoadingScreen from './App/Authentication'
const BeforeSignin = createStackNavigator(
{
SignIn: {
screen: SignIn,
},
},
{
headermode: 'none',
initialRouteName: 'Login',
}
)
const AfterSignin = createStackNavigator(
{
Dashboard: {
screen: Dashboard,
},
},
{
headermode: 'none',
initialRouteName: 'Dashboard',
}
)
const AppNavigator = createStackNavigator(
{
Auth: BeforeSignin,
App: AfterSignin,
AuthLoadingScreen: AuthLoadingScreen,
},
{
headermode: 'none',
initialRouteName: 'Auth',
}
)
export default createAppContainer(AppNavigator)
我检查了几次,似乎无法理解错误的原因
点的使用.
指同级目录。
更改自:
import Signin from "./App/SignIn"
至:
import Signin from "./SignIn"
但如果你坚持使用
App/Signin
格式,可以参考以下文章: Absolute imports with Create React App 。
@Rishail Siddiqui,尝试更改导入方法,例如
import SignIn from './SignIn';
import Dashboard from './DashBoard';
import AuthLoadingScreen from './Authentication';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.