【问题标题】:angular ui-router, html5 mode always refreshes to /angular ui-router,html5模式总是刷新到/
【发布时间】:2015-06-09 07:07:18
【问题描述】:

我正在尝试在 Angular 中使用 html5mode,以便我可以为 http:/myhost/products 之类的页面添加书签(其中 /products 是由 $stateProviderRef.state(xxx) 定义的路由)。

为此我已经

  • 在我的应用配置中添加了 $locationProvider.html5Mode(true)
  • 在我的 index.html 中添加了 'base href="/"'(带有 )
  • 在 node.js 中将 catch all rewrite 添加到我的 server.js

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

  • 重启节点服务器

所以应用程序启动正常,所有导航正常,我可以转到http://myhost/products,一切正常。

但是,如果此时我按刷新,我将被重定向回索引页面。在我看来,ui-router 要么丢失了路径(/products),要么我错过了配置/设置中的某些内容

我一直在浏览 StackOverflow 上的问题,直到眼睛流血为止,但类似问题的所有解决方案都是我已经做过的事情(base=、重定向等)

其他人有这个问题并解决了吗?如果您能分享您的发现,将不胜感激。

谢谢

【问题讨论】:

    标签: angularjs node.js express angular-ui-router


    【解决方案1】:

    你不应该这样在服务器中重定向

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

    相反,发送相同的 index.html

    app.route('/*')
        .get(function(req, res) {
          res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
        });
    

    看看这个生成器了解更多

    https://github.com/DaftMonk/generator-angular-fullstack/blob/master/app/templates/server/routes.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 2017-06-24
      • 2014-09-05
      • 1970-01-01
      • 2014-05-26
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多