【发布时间】:2020-01-02 23:11:31
【问题描述】:
我尝试添加括号或分号,但出现错误:
语法错误:意外的标记
如何实现此功能并使其正常工作?我的完整代码:
还有,为什么不工作?
从'react'导入反应,{组件}; 从'react-native'导入{文本,ActivityIndicator,视图,按钮,TextInput,样式表,TouchableOpacity,尺寸,AppRegistry,平台,状态栏,异步存储}; 导入 { createAppContainer, createSwitchNavigator } from 'react-navigation'; 从'react-navigation-stack'导入{createStackNavigator};
const userInfo = {username: 'admin', password: 'pass12345'}
class HomeScreen extends Component {
static navigationOptions = { header: null }
constructor(props) {
super(props);
this.state = { username: '', password: '' }
}
render() {
return (
<View style={styles.container}>
<Text>News</Text>
<TextInput
style={styles.input}
placeholder={'Username'}
onChangeText={(username)=>this.setState({username})}
value={this.state.username}
autoCapitalize="none"
/>
<TextInput
style={styles.input}
placeholder={'Password'}
secureTextEntry={true}
onChangeText={(password)=>this.setState({password})}
value={this.state.password}
/>
<View>
<TouchableOpacity
style={styles.btnLogin}
onPress={this._login}
//onPress={() => this.props.navigation.navigate('Details')}
>
<Text style= {styles.text}>Login</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => alert("Signup Works")} >
<Text style={styles.btnTxt}>Signup</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
class DetailsScreen extends Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
</View>
);
}
}
const RootStack = createStackNavigator({
//Home: HomeScreen,
Details: DetailsScreen
}, {//initialRouteName: 'Home'} );
defaultNavigationOptions:{
headerStyle:{
backgroundColor: '#1e90ff'
},
headerTitleStyle: {
textAlign:'center',
flex: 1
}
}
},
};
const AuthStack = createStackNavigator({Home: HomeScreen});
class AuthLoadingScreen extends Component {
constructor (props){
super(props);
this._loadData();
}
render(){
return(
<View>
<ActivityIndicator/>
<StatusBar barStyle="default"/>
</View>
);
}
_loadData = async() => {
const isLogged = await AsyncStorage.getItem('isLoggedIn');
this.prop.navigation.navigate(isLoggedIn !== '1'? 'Auth':'App');
}
}
export default createAppContainer(
createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: RootStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
)
);
_login = async() => {
if (userInfo.username === this.state.username && userInfo.password === this.state.password) {
//alert('Logged In');
await AsyncStorage.setItem('isLoggedIn', '1');
this.props.navigation.navigate('Details')
} else {
alert('Username or Password is incorrect.');
}
}
const styles = StyleSheet.create({
container:{ flex:1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#00bfff', },
btnLogin: { flex: 0.5, height: 45, borderRadius: 25, fontSize: 16, justifyContent: 'center', marginTop: 20 },
btnTxt:{ },
input:{ justifyContent: 'center', width:"90%", padding: 15, marginBottom: 10 }
});
【问题讨论】:
-
附上的图片是expo错误
-
修复格式和/或使用像 PhpStorm/IntelliJ 这样的 IDE。它会立即指出错误。
-
我看到了错误,谢谢,但是我该如何解决呢? @mpen
-
已经有人回复你了。只需修复您不匹配的大括号即可。
标签: reactjs react-native