本文介绍了Html5Mode - 刷新angularjs页面问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个angularJS应用程序,其中有URL格式(在dev) -

I am working on an angularJS app, which has URLs in the format (in dev) -

http://localhost:6001/#/home/index
http://localhost:6001/#/content/index

由于这个应用程序,也将在桌面浏览器,我想要的URL没有'#',所以我添加了在我的应用程序的配置部分 -

As this app, will also be available on desktop browser, I wanted the URLs to be without '#', so I added this in my app's config section -

$locationProvider.html5Mode(true);

其中正常工作和网址不显示'#'。但现在有一个问题。在浏览器中刷新特定页面时,现在出现此错误 -

Which works fine and URLs don't show '#'. But there is a problem now. On refreshing a particular page in browser, now I get this error -

Cannot GET /home/index

是否需要进行任何其他更改才能使此工作?

Are there any other changes required to get this working?

strong>注意:此应用使用cordova CLI并托管​​在Node.js服务器上。

Note: This app is using cordova CLI and is hosted on a Node.js server.

推荐答案

以放入您的路线:

app.get(*, function(req,res) {
  //return the main index file
});

这将导致任何不匹配的路由服务索引页,并从那里角度照顾这条路线。

This will cause any unmatched route to serve the index page and from there angular take care of this route.

这篇关于Html5Mode - 刷新angularjs页面问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 18:01