问题
I created a simple router for my app in Svelte. It is working if I'm accessing the link from the nav bar. If I reload the page, it give me 404.. why ?
<Router url="{url}">
<nav>
<Link to="/">Home</Link>
<Link to="charts">About</Link>
</nav>
<div>
<Route path="charts" component="{About}" />
<Route path="/"><Home /></Route>
</div>
</Router>
After reload: This localhost page can’t be found No webpage was found for the web address: http://localhost:5000/charts
回答1:
You must make sure your server is serving the index.html
for every route path and not just for /
.
If you e.g. are using sirv
with one of the Svelte starter projects you can add the --single
flag to the script.
"scripts": {
"start": "sirv public --single",
"start:dev": "sirv public --dev --single"
},
回答2:
First of all, we won't be able to access your link as it is a local link only accessible from your local machine.
However my bet would be a redirection issue. You are trying to access the chart
file on your server, that arguably doesn't exists as your router is supposed to do that job.
You need to configure your server so that every route is redirected to your index file, and then the router will be able to do it's job by analyzing the url.
来源:https://stackoverflow.com/questions/57339349/svelte-route-gives-me-404