角UI路由器,HTML5模式始终刷新到/ [英] angular ui-router, html5 mode always refreshes to /

查看:155
本文介绍了角UI路由器,HTML5模式始终刷新到/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在角使用html5mode,这样我可以为网页添加书签,如http:/为myhost /产品(其中/产品是$ 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(真),以我的应用程序配置

  • 加入'基地HREF =/(与在<?>)我的index.html

  • 添加了包罗万象的重写我的server.js在node.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('*',函数(REQ,RES){
 res.redirect('/');
});

app.get('*', function(req, res) { res.redirect('/'); });

重新启动了节点服务器

那么,什么情况是,应用程序启动正常,所有导航的作品,我可以去的http://为m​​yhost /产品和一切正常。

so what happens is that the app starts ok, all navigation works, I can go to http://myhost/products and everything works well.

不过,如果我preSS刷新在这一点上,我重定向到索引页面。在我看来,如果UI路由器或者是失去路径(/产品)或我已经错过了在config /设置的东西。

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

我已浏览通过计算器,直到我的眼睛都在流血的问题,但所有的解决方案,以类似的问题是,我已经做过的事情(基地=,重定向等)

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)

任何人有这个问题,并解决它?将非常AP preciated,如果你可以分享你的发现。

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

<一个href=\"https://github.com/DaftMonk/generator-angular-fullstack/blob/master/app/templates/server/routes.js\" rel=\"nofollow\">https://github.com/DaftMonk/generator-angular-fullstack/blob/master/app/templates/server/routes.js

这篇关于角UI路由器,HTML5模式始终刷新到/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆