如何在AngularJS的Rails 4中修正设置根路由?

Sas*_* B. 4 routing ruby-on-rails angularjs

我在使用AngularJS集成Rails路由时遇到了麻烦.

当我在浏览器输入中转到"localhost:3000"时,它会将我重定向到"localhost:3000 /#/ auth/sign_in"

但'sign_in'模板无法渲染.

然后,如果我转到"localhost:3000 /#/",它会将我重定向到"localhost:3000 /#/ auth/sign_in"并向右渲染"sign_in"模板.

如何修复它,当我去"localhost:3000"然后渲染'sign_in'?

路由器代码在这里:http://pastie.org/8917505

谢谢!

Sas*_* B. 9

我找到了解决方案.

您需要为AngularJS 启用HTML5模式:

angular.module('app').config(['$routeProvider', '$locationProvider',
  function ($routeProvider, $locationProvider) { 
  ...
    $locationProvider.html5Mode(true);
  }]
)
Run Code Online (Sandbox Code Playgroud)

您还需要添加<base>html标记:

<html>
  <head>
    <base href="/">
      ...
  </head>
</html>
Run Code Online (Sandbox Code Playgroud)

当您再次在浏览器URL输入中按Enter键时,它修复了Angular对URL路径的错误处理.如果没有这个,最后的"域名"将会重复:

本地主机:3000/auth /中sign_in

本地主机:3000 /认证/认证/ sign_in

...

您还需要在服务器端(在Rails中)为所有SPA路由编写正确的路由:

配置/ routes.rb中

  root 'application#main'

  get '*path', to: 'application#main'
Run Code Online (Sandbox Code Playgroud)