问题
Is there an equivalent function to grab the currentPath in the console with the new router in ember?
Before, I was able to do this:
App.router.get("currentPath")
However, it seems with the new router, you can't access the router in the same fashion. In fact App.router simply returns undefined.
Any help here would be greatly appreciated.
edit:
On a related note, because I can no longer access App.router
and its properties, I can't figure out how to manually trigger state changes. For example,
App.router.transitionTo("some.state")
is no longer viable. What is everyone doing instead of this now?
回答1:
I found the answer buried in github issues. Re-posting here for posterity.
App.container.lookup('router:main').router
..returns the router. You can access transitionTo and handleURL to manually trigger state changes. Not exactly sure how to get the current state like before, but it seems like you can access the currentHandlerInfos property on the router to get an array of the current handlers (duh).
Hope this prevents someone else from ripping out their hair.
回答2:
As of the latest build, you can find the router and change state like so:
App.Router.router.transitionTo('posts.comments');
or
App.Router.router.handleURL('/posts/comments');
I don't know if this is the recommended way (the router is very much a moving target at this point), but it works for now.
回答3:
As of Ember 3.9, here is the new Router Service API docs.
https://api.emberjs.com/ember/3.9/classes/RouterService
import Component from '@ember/component';
import { inject } from '@ember/service';
export default Component.extend({
router: inject(),
actions: {
next() {
this.router.transitionTo('other.route');
}
}
});
Also here is an answer I posted about how to get the currentPath
on the router service.
https://stackoverflow.com/a/55580684/4044548
回答4:
I think this should do
function getCurrentPath(){
str = "currentState.parentState"
arr = []
arr.push(App.router.get("currentState.name"))
while(true){
name = App.router.get(str+".name");
arr.push(name)
str += ".parentState";
if(name == "root"){
break;
}
}
return arr.reverse().join(".")
}
currentPath = getCurrentPath();
来源:https://stackoverflow.com/questions/14109000/access-the-new-ember-router-in-the-console