The route i came from, or previous route

▼魔方 西西 提交于 2019-12-22 09:47:00

问题


When i visit one route and then go to another, is where a way to know which route i came from?
I tried document.referrer - this doesn't work. Googling hadn't brought any answers either..

I set up this fiddle for example and testing..


回答1:


There is no direct solution using the API. Meaning, this information is not part of the public API. However, you can cache this information yourself.

App.set('lastRoutes', []);

App.BaseRoute = Ember.Route.extend({
    setupController: function() {
        this._super.apply(this, arguments);
        App.get('lastRoutes').pushObject(this.get('routeName'));
    }
});

Use "setupController", rather than "enter", because when you transition from /users/1 to /users/2 for instance, the enter/exit methods are not executed, whereas the setupController gets executed every time (and is part of the public API). You should refine it if you have nested routes, because this will add the intermediate routes as well. But for your example, it works well.



来源:https://stackoverflow.com/questions/14831668/the-route-i-came-from-or-previous-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!