简体   繁体   English

使用angular-ui-router处理没有hashbang的路由

[英]Handling routes without hashbang using angular-ui-router

I've searched a lot online for an answer to this but haven't found anything yet. 我在网上搜索了很多答案,但还没有找到任何答案。

Right now I have this code in my file that I thought would handle all routes/states besides the ones I've specified: 现在我在我的文件中有这个代码,我认为除了我指定的那些之外我还会处理所有路由/状态:

$urlRouterProvider.otherwise('/');

However, this functionality only works for paths like localhost:1337/#/stuff , but if I type in localhost:1337/stuff , I get an ugly internal server error. 但是,此功能仅适用于localhost:1337/#/stuff类的路径,但如果我输入localhost:1337/stuff ,我会收到一个丑陋的内部服务器错误。

Ideally, I'd like localhost:1337/stuff or any other URL without a hashbang to redirect to localhost:1337/#/ (my app's homepage). 理想情况下,我喜欢localhost:1337/stuff或没有hashbang的任何其他URL重定向到localhost:1337/#/ (我的应用程序的主页)。

Any thoughts on how I can accomplish this? 有关如何实现这一目标的任何想法?

You need to set up two things: 你需要设置两件事:

  1. A so-called " html5Mode " in your AngularJS configuration. AngularJS配置中的所谓“ html5Mode ”。 That's the easy part: $locationProvider.html5Mode(true); 这是最简单的部分: $locationProvider.html5Mode(true);
  2. Set rewrites on your server so any request renders only your main html page. 在您的服务器上设置重写,以便任何请求只呈现您的主html页面。

From AngularJS docs on routing : 来自AngularJS 关于路由的文档

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (eg index.html). 使用此模式需要在服务器端重写URL,基本上您必须重写所有链接到应用程序的入口点(例如index.html)。

You can enable html5 mode within your app.js, which will also remove the hashbang from your links in your project ( localhost:1337/stuff instead of localhost:1337/#/stuff ). 您可以在app.js中启用html5模式,这也将从您项目中的链接中删除hashbang( localhost:1337/stuff而不是localhost:1337/#/stuff )。

If this is not the behavior you want, then you'll have to do the rewrites on the server side. 如果这不是您想要的行为,那么您将不得不在服务器端进行重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM