简体   繁体   English

Svelte 路线给了我 404

[英]Svelte route gives me 404

I created a simple router for my app in Svelte.我在 Svelte 中为我的应用程序创建了一个简单的路由器。 It is working if I'm accessing the link from the nav bar.如果我从导航栏访问链接,它就可以工作。 If I reload the page, it give me 404.. why ?如果我重新加载页面,它会给我 404.. 为什么?

<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重新加载后:找不到此本地主机页面未找到该网址的网页: http://localhost:5000/charts

As mentioned in one of the comments above, if using roll-up, the following combination of scripts will work when calling npm run dev如上面的评论之一所述,如果使用汇总,则在调用npm run dev时,以下脚本组合将起作用

"scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public --single"
  }

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. 您正在尝试访问服务器上的chart文件,该文件可能不存在,因为您的路由器应该执行该工作。

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. 您需要配置服务器,以便将每条路由都重定向到索引文件,然后路由器将能够通过分析URL来完成其工作。

You must make sure your server is serving the index.html for every route path and not just for / . 您必须确保服务器为每个路由路径都提供index.html ,而不仅仅是/

If you eg are using sirv with one of the Svelte starter projects you can add the --single flag to the script. 如果您将sirv与Svelte入门项目之一一起使用,则可以在脚本中添加--single标志。

"scripts": {
  "start": "sirv public --single",
  "start:dev": "sirv public --dev --single"
},

This is for those who want to host the svelte apps, If your hosting provider supports adding redirect or rewrite rules then you can do this to serve the index.html for any request you make which the router can handle.这是为那些想要托管 svelte 应用程序的人准备的,如果您的托管服务提供商支持添加重定向或重写规则,那么您可以这样做来为路由器可以处理的任何请求提供index.html服务。

For example, Set source as /* the / refers to the path and the wildcard * refers to match arbitrary request paths.例如,Set source as /* /指路径,通配符*指匹配任意请求路径。

and then set your destination to just / that's it.然后将您的目的地设置为只是/就是这样。 don't set the destination as /index.html or else all requests will open your home page for example if you try to open www.example.com/about then it will open www.example.com/index.html不要将目的地设置为/index.html否则所有请求都会打开您的主页,例如,如果您尝试打开www.example.com/about那么它将打开www.example.com/index.html

and set the action as Rewrite instead of Redirect并将操作设置为Rewrite而不是Redirect

and this also solves the problem of refreshing the site and opening the URL directly from browswer并且这也解决了刷新站点和直接从浏览器打开URL的问题

change改变
"start": "sirv public --no-clear" "start": "sirv public --no-clear"

To this "start": "sirv public -s --no-clear"对此“开始”:“sirv public -s --no-clear”

in your package.json file , it worked for me在你的 package.json 文件中,它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM