transitionToRoute('route') From Inside Component

后端 未结 6 1171
逝去的感伤
逝去的感伤 2021-01-04 02:18

How do I transition to a route pragmatically from inside a component action?

I tried to use @get(\'controller\').transitionToRoute(\'images\'

6条回答
  •  情歌与酒
    2021-01-04 02:30

    A lot of things changed in Ember since the original post. So maybe today the best option would be to pass down to the component a route action that takes care of the transition (maybe using the fancy addon ember-cli-route-action.

    Otherwise you can create an initializer with ember g initializer router and inside put in there a code like this one

    export function initialize (application) {
       application.inject('route', 'router', 'router:main')
       application.inject('component', 'router', 'router:main') 
    }
    
    export default {
      name: 'router',
      initialize
    }
    

    This way you can access the router in your component with this.get('router') and, for instance, perform a transition

     this.get('router').transitionTo('images')
    

提交回复
热议问题