我有反应网站,那里的路由是用反应-路由器-多玛和本地主机,一切都很好。但是,在我的生产服务器上,我使用哪个web服务器(Nginx、Apache2或Apache2 + Nginx)与嵌套链接(如https://example.com/admin/list )没有关系,但我得到了一个错误:
Uncaught SyntaxError: Unexpected token '<' 2.a3028c0a.chunk.js:1
Uncaught SyntaxError: Unexpected token '<' main.1b4093c1.chunk.js:1
Manifest: Line: 1, column: 1, Syntax error. manifest.json:1当您通过按钮进入嵌套链接时,一切正常。
<NavLink to="/admin/asics">但是在刷新或直接运行之后,我得到了这个错误。
有了像https://example.com/admin这样的单一链接,一切都很好。
嵌套路由是如何完成的
App.js
<BrowserRouter basename="/">
<Switch>
<Route exact path="/" component={Miners} />
<Route path="/admin" component={Admin} />
<Route path="/gratitude" component={Gratitude} />
<Route path="/firmware" component={Firmware} />
<Redirect to="/" />
</Switch>
</BrowserRouter>Admin.js
<BrowserRouter>
<Switch>
<Route exact path="/admin/asics" component={Panel} />
<Route path="/admin/advertising" component={AdminImgPanel} />
<Route path="/admin/statistics" component={AdminStat} />
<Redirect to="/admin/asics" />
</Switch>
</BrowserRouter>我想这可能是web服务器的问题,所以我尝试了Apache2和.htaccess。
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L] 以及在conf中使用此代码的Nginx。
try_files $uri /index.html;两种方法都一样。
发布于 2021-09-22 14:35:46
问题是在package.json中,主页字段中有一个点。
"homepage": "."你需要把它改成斜杠
"homepage": "/"https://stackoverflow.com/questions/69235005
复制相似问题