如何在 Rails 4 中为 AngularJS 更正设置根路由? [英] How to correct set root route in Rails 4 for AngularJS?
问题描述
我在将 Rails 路由与 AngularJS 集成时遇到了麻烦.
I have a troubles with integrating Rails routing with AngularJS.
当我在浏览器输入中转到localhost:3000"时,它会将我重定向到localhost:3000/#/auth/sign_in"
When I go to "localhost:3000" in browser input, it redirect me to the "localhost:3000/#/auth/sign_in"
但sign_in"模板未呈现.
but 'sign_in' template not render.
然后,如果我转到localhost:3000/#/",它会将我重定向到localhost:3000/#/auth/sign_in"并正确呈现sign_in"模板.
Then, secondary if I go to the "localhost:3000/#/", it redirect me to the "localhost:3000/#/auth/sign_in" and render 'sign_in' template right.
如何修复它,当我转到localhost:3000"然后呈现 'sign_in' 时?
How to fix it, when I go to "localhost:3000" then render 'sign_in' ?
此处的路由器代码:http://pastie.org/8917505
谢谢!
推荐答案
我找到了解决方案.
您需要为 AngularJS 启用 HTML5 模式:
You need to enable HTML5 mode for AngularJS:
angular.module('app').config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
...
$locationProvider.html5Mode(true);
}]
)
您还需要添加
html 标签:
You also need to add the <base>
html tag:
<html>
<head>
<base href="/">
...
</head>
</html>
它修复了当您再次在浏览器 URL 输入中按 Enter 时,Angular 对 URL-path 的错误处理.如果没有这个,最后一个域"将被重复:
It fixes Angular's incorrect processing of URL-path when you press Enter in the browser URL input again. Without this, the last 'domain' will be repeated:
本地主机:3000/auth/sign_in
localhost:3000/auth/sign_in
本地主机:3000/auth/auth/sign_in
localhost:3000/auth/auth/sign_in
...
您还需要在服务器端(在 Rails 中)为所有 SPA 路由编写正确的路由:
You also need to write correct route on server side (in Rails) for all SPA routes:
config/routes.rb
root 'application#main'
get '*path', to: 'application#main'
这篇关于如何在 Rails 4 中为 AngularJS 更正设置根路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!