如何在没有 URL 中的 #(哈希)的情况下在 Elm 中执行 routing/navigation?
How to do routing/navigation in Elm without the # (hash) in the URL?
使用 UrlParser.parseHash
函数我能够成功解析以下 url:
http://localhost:8000/MyRepl.elm/#home/something-else
行为符合预期,当我将其复制粘贴到浏览器中并按下回车键时 - 应用会加载相应的视图。
但现在我想删除 #
,为此我使用了 UrlParser.parsePath
函数。我保留了其余代码,与以前完全一样 - 但由于某些原因,这不起作用。
当我复制粘贴并按下回车键时:
http://localhost:8000/MyRepl.elm/home/something-else
- 注意没有 #
.
浏览器创建对 elm -reactor localhost 服务器的直接请求。
没有路由发生。 elm reactor server returns a 404 - 好像没有名为 /MyRepl.elm/home/something-else
的文件
但是没有 #
的路由应该是可能的,因为 http://package.elm-lang.org/packages
- 文档站点是用 elm 编写的,url 中没有 #
,如您所见.
问题:
有人遇到过同样的问题吗?有什么解决办法吗?
或者你能告诉我一个没有 #
的导航可以正常工作的 repo 吗?
您需要一个后端来为每个请求提供索引页面服务。在为索引页面提供服务后,路由将照常在 Elm 中进行。
例如,在 express 中它看起来像这样:
router.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
router.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
Elm 反应器不支持这个。
如果你使用的是 webpack,你可以对 historyApiFallback
属性做同样的事情 How to tell webpack dev server to serve index.html for any route
使用 UrlParser.parseHash
函数我能够成功解析以下 url:
http://localhost:8000/MyRepl.elm/#home/something-else
行为符合预期,当我将其复制粘贴到浏览器中并按下回车键时 - 应用会加载相应的视图。
但现在我想删除 #
,为此我使用了 UrlParser.parsePath
函数。我保留了其余代码,与以前完全一样 - 但由于某些原因,这不起作用。
当我复制粘贴并按下回车键时:
http://localhost:8000/MyRepl.elm/home/something-else
- 注意没有 #
.
浏览器创建对 elm -reactor localhost 服务器的直接请求。
没有路由发生。 elm reactor server returns a 404 - 好像没有名为 /MyRepl.elm/home/something-else
但是没有 #
的路由应该是可能的,因为 http://package.elm-lang.org/packages
- 文档站点是用 elm 编写的,url 中没有 #
,如您所见.
问题:
有人遇到过同样的问题吗?有什么解决办法吗?
或者你能告诉我一个没有 #
的导航可以正常工作的 repo 吗?
您需要一个后端来为每个请求提供索引页面服务。在为索引页面提供服务后,路由将照常在 Elm 中进行。
例如,在 express 中它看起来像这样:
router.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
router.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
Elm 反应器不支持这个。
如果你使用的是 webpack,你可以对 historyApiFallback
属性做同样的事情 How to tell webpack dev server to serve index.html for any route