AngularJS Route 在手动刷新时中断 [英] AngularJS Route breaks on manual refresh

查看:26
本文介绍了AngularJS Route 在手动刷新时中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在config方法中,我定义了一些路由如下:

In the config method, I have some routes defined as follows:

$locationProvider.html5Mode(true);

$routeProvider.when('/', {
  ...
});

$routeProvider.when('/front', {
  ...
});

$routeProvider.when('/user/account', {
  ...
});

使用 <a href=""> 标签浏览应用时,一切正常.但是,当我转到 /user/account 并手动刷新浏览器时,它会将 /account 弹出路由并将我重定向到 /user>,由于没有为 /user 定义路由,所以破坏了渲染过程.

Everything works fine when navigating through the app using <a href=""> tags. However, when I go to /user/account and manually refresh my browser, it pops the /account off the route and redirects me to /user, breaking the rendering in the process since there is no route defined for /user.

我注意到当我将路由配置为 /account 时不会发生这种情况,但这并没有解决真正的问题.所以我设置了以下全能(在上面的路线下面)来记录正在发生的事情:

I noticed this does not happen when I configure the route to be /account only, but this is not fixing the real issue. So I set up the following catch-all (below the routes above) to log what's happening:

$routeProvider.otherwise({
  redirectTo: function() {
    console.log('bumped', arguments);

    return '/';
  }
});

并看到它试图匹配 /account 而不是 /user/account.这是怎么回事?

and saw it was trying to match /account instead of /user/account. What is going on here?

我使用的是 Angular 1.1.5.服务器在所有请求上适当地返回 index.html(对于 HTML5 模式),所以这似乎是一个客户端问题.

I am on Angular 1.1.5. The server returns index.html appropriately on all requests (for HTML5 mode), so it seems like a client-side issue.

如何正确配置这条路由?

How can I configure this route correctly?

编辑

事实证明这是 1.1.5 内核中的错误.

Turns out this is a bug in 1.1.5 core.

https://github.com/angular/angular.js/issues/2799

在这里打补丁

https://github.com/IgorMinar/angular.js/commit/2bc62ce98f893377bfd76ae211c8af027bb74c1d"/a>

https://github.com/IgorMinar/angular.js/commit/2bc62ce98f893377bfd76ae211c8af027bb74c1d

推荐答案

最终使用 ui-router 解决了这个问题.

Ended up using ui-router which solved this issue.

这篇关于AngularJS Route 在手动刷新时中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆