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

标签 javascript angularjs angular-ui-router

我在网上搜索了很多答案,但还没有找到任何答案。

现在我的文件中有这段代码,我认为它可以处理除我指定的之外的所有路线/状态:

$urlRouterProvider.otherwise('/');

然而,此功能仅适用于像 localhost:1337/#/stuff 这样的路径,但如果我输入 localhost:1337/stuff,我会得到一个丑陋的内部服务器错误。

理想情况下,我希望 localhost:1337/stuff 或任何其他没有 hashbang 的 URL 重定向到 localhost:1337/#/(我的应用程序的主页)。

我有什么想法可以实现吗?

最佳答案

你需要设置两件事:

  1. AngularJS 配置中所谓的“html5Mode”。这是简单的部分:$locationProvider.html5Mode(true);
  2. 在您的服务器上设置重写,以便任何请求只呈现您的主 html 页面。

来自 AngularJS docs on routing :

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html).

关于javascript - 使用 angular-ui-router 处理没有 hashbang 的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094870/

相关文章:

javascript - 如何在href中使用js的值?

javascript - 日期对象行为

javascript - javascript for循环内的异步进程

javascript - 我如何以非递归方式重写此 CallExpression?

javascript - Angular : ng-repeat not working

javascript - 模态状态angularjs

javascript - Angularjs $interval.flush()

javascript - 我可以将独立作用域和继承作用域结合起来吗?

javascript - 用户界面路由器 : composite view render view once

AngularJS 使用 ui-router 通过 ng-click 更改路由