我正在创建React Web应用,并且该应用内的动态路由会返回这些错误。静态路由可以正常工作。我得到了错误:未捕获到的SyntaxError:意外令牌'
import React, { Component } from 'react';
import './NavBar.css';
import Axios from 'axios';
import { observer } from 'mobx-react';
import UserStore from '../../src/Stores/UserStore';
class NavBar extends Component {
constructor(props) {
super(props);
this.state = {
results: {},
apiLoading: false,
message: ''
};
}
async componentDidMount() {
try{ //check if the user is logged in
let res = await fetch('/isLoggedIn', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
let result = await res.json();
if (result && result.success) {
UserStore.loading = false;
UserStore.isLoggedIn = true;
UserStore.username = result.username;
}else{
UserStore.loading = false;
UserStore.isLoggedIn = false;
}
}
catch(error){
UserStore.loading = false;
UserStore.isLoggedIn = false;
}
}
render () {
if(UserStore.loading) {
return(
<div>
<p>Loading, please wait...</p>
</div>
);
}else{
if(UserStore.isLoggedIn){
let hrefString = '/account/' + UserStore.username;
return(
<div className="navigation">
<div className="HomeButton">
<a href="/"><h3>Stock Track</h3></a>
<i id="#icon" className="fas fa-globe"></i>
</div>
<div className="NavButtons">
<a href={hrefString}>My Account</a>
<a href="/lessons">Lessons</a>
</div>
</div>
);
}else{
return(
<div className="navigation">
<div className="HomeButton">
<a href="/"><h3>Stock Track</h3></a>
<i id="#icon" className="fas fa-globe"></i>
</div>
<div className="NavButtons">
<a href="/register">Register</a>
<a href="/login">Login</a>
<a href="/lessons">Lessons</a>
</div>
</div>
);
}
}
}
在我的App.js中,我的路由器包括:
<Router>
<Switch>
{/* This route doesn't return the error */}
<Route exact path="/marketoverview" component={marketOverview} />
{/* But this route returns the error */}
<Route path='/account/:username' component={MyAccount}/>
</Switch>
</Router>
谢谢您的建议
检查public/
文件夹中的manifest.json文件。如果您没有,请创建一个并添加此默认内容。
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
在终端中停止服务器,然后再次运行npm start
。