gpt4 book ai didi

ruby-on-rails - 如何在 Rails/Angularjs html5 模式下删除重定向

转载 作者:行者123 更新时间:2023-12-03 15:57:31 25 4
gpt4 key购买 nike

使用 html 5 模式和 rails 时,如何避免每次用户刷新 angularjs 页面时性能下降?我正在使用 Angularjs html5 模式 Ruby on Rails 路由重定向。如果我访问我的设置 http://localhost:3000/users网址立即变为 http://localhost:3000/?goto=users然后重定向到 http://localhost:3000/users .使用 html5 模式时,如何避免这种性质的重定向?

铁路路线

myApp::Application.routes.draw do
namespace :api, defaults: {format: :json} do
namespace :v1 do
resources :users
end
end

# Re-route all angular requests to the angular router
get "/*path" => redirect("/?goto=%{path}")
root to: 'application#home'

结尾

Angular 路线
myApp.config(function($routeProvider, $locationProvider, $httpProvider) {
$httpProvider.defaults.headers.common['X-CSRF-Token'] =
$('meta[name=csrf-token]').attr('content');
$routeProvider.
when('/', {
templateUrl: '../assets/mainIndex.html',
controller: 'MainController',
redirectTo:
function(current, path, search){
if(search.goto){
// if we were passed in a search param, and it has a path
// to redirect to, then redirect to that path
return "/" + search.goto;
}
else{
// else just redirect back to this location
// angular is smart enough to only do this once.
return "/";
}
}
}).when('/users', {
templateUrl: '../assets/users/userIndex.html',
controller: 'UsersController'
}).otherwise({
redirectTo:'/'
});
$locationProvider.html5Mode(true);
});

以上来自本博客: http://omarriott.com/aux/angularjs-html5-routing-rails/

最佳答案

只需使用此 rails 路线:

  # Route all valid requests to AngularJS
root to: 'application#home'
get "*path" => "application#home"

和 rails action_controller:
  # We'll just use this as a launch point for our App
def home
# Render just the layout since this application is Angular driven
# our layout/application has all the angular logic and our controllers
# have no views for themselves. We just need a place to launch from
# and this happens to be it. So we have no view (thus :nothing => true)
# but we still want the layout (since it has the App bootstrap code)
render :layout => 'application', :nothing => true
end

现在不需要处理来自 angularjs 路由器的重定向。只需为您的页面制作简单的路由即可。

关于ruby-on-rails - 如何在 Rails/Angularjs html5 模式下删除重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com