angular ui-router, html5 模式总是刷新为/ [英] angular ui-router, html5 mode always refreshes to /
问题描述
我正在尝试在 angular 中使用 html5mode,以便我可以为 http:/myhost/products 之类的页面添加书签(其中/products 是由 $stateProviderRef.state(xxx) 定义的路由).
I am trying to use html5mode in angular, so that I can bookmark a page like http:/myhost/products (where /products is a route defined by$stateProviderRef.state(xxx) ).
为此我已经
- 在我的应用配置中添加了 $locationProvider.html5Mode(true)
- 在我的 index.html 中添加了 'base href="/"'(带有 <>)
在 node.js 中添加了所有重写到我的 server.js 的捕获
- added $locationProvider.html5Mode(true) to my app config
- added 'base href="/"' (with the <>) to my index.html
added the catch all rewrite to my server.js in node.js
app.get('*', function(req, res) {res.redirect('/');});
app.get('*', function(req, res) { res.redirect('/'); });
重启节点服务器
所以发生的情况是应用程序启动正常,所有导航正常,我可以转到 http://myhost/products 一切正常.
so what happens is that the app starts ok, all navigation works, I can go to http://myhost/products and everything works well.
但是,如果我此时按下刷新,我将被重定向回索引页面.在我看来,ui-router 要么丢失了路径(/products),要么我在配置/设置中遗漏了一些东西
However, if I press refresh at this point, I am redirected back to the index page. Looks to me as if ui-router is either losing the path (/products) or I have missed something in the config / setup
我一直在浏览 StackOverflow 上的问题,直到我的眼睛都流血了,但所有类似问题的解决方案都是我已经做过的事情(base=、重定向等)
I have been browsing through the questions on StackOverflow until my eyes are bleeding, but all of the solutions to similar problems are things that I've already done (base=, redirect etc)
其他人有这个问题并解决了吗?如果您能分享您的发现,将不胜感激.
Anyone else has this problem and solved it ? Would be much appreciated if you could share your findings.
谢谢
推荐答案
你不应该像那样在服务器中重定向
You shouldn't redirect in server like that
app.get('*', function(req, res) { res.redirect('/'); });
相反,发送相同的 index.html
Instead, send same index.html
app.route('/*')
.get(function(req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
看看这个生成器了解更多
Take a look at this generator for more
https://github.com/DaftMonk/generator-angular-fullstack/blob/master/app/templates/server/routes.js
这篇关于angular ui-router, html5 模式总是刷新为/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!