简体   繁体   English

Ember 2 - 过渡到组件的路径

[英]Ember 2 - Transition to route through component

I am trying to make a pagination component for ember 2 app. 我正在尝试为ember 2 app制作一个分页组件。 Since it is a general purpose pagination component, I wanted to use it on many different routes. 由于它是一个通用的分页组件,我想在许多不同的路线上使用它。 Because of that I need to somehow (in my mind) provide a name of my route for pagination links to work. 因此,我需要以某种方式(在我的脑海中)提供我的路线名称,以便分页链接起作用。

I can pass in my route name (string) to the component easily: {{pagination-component myRoute='myCurrentRouteName'}} . 我可以轻松地将我的路由名称(字符串)传递给组件: {{pagination-component myRoute='myCurrentRouteName'}} And inside of component: {{link-to myRoute (query-params page=1)}} 在组件内部: {{link-to myRoute (query-params page=1)}}

It works well for going to pages like: << First, < Previous, Next > , Last >>. 它适用于转到以下页面:<< First,<Previous,Next>,Last >>。

But I also wanted to have a select box with options pointing to all the pages, where if user selects a page, I can transition into that route with queryParams similar to this: myRoute?page=selectedPage . 但是我也希望有一个选项框,其中的选项指向所有页面,如果用户选择页面,我可以使用与此类似的queryParams转换到该路径:myRoute myRoute?page=selectedPage All the tutorials for ember say that transitioning within the component is a no-no. ember的所有教程都说在组件内进行转换是禁止的。

But how do I do it instead, given that I want my pagination to be generic and I don't want to have action within every single route that deals with pagination and provide same exact transitioning? 但是我该怎么做呢,因为我希望我的分页是通用的,我不想在处理分页并提供相同的转换的每一条路线中都采取行动?

So far I found that I can inject the '-routing' into the component, which can work for transitioning within the component, but it also doesn't quiet work for some reason. 到目前为止,我发现我可以将“-routing”注入到组件中,这可以在组件内进行转换,但由于某些原因它也不会安静工作。 Plus people say that it is private and unreliable. 而且人们说它是私密且不可靠的。 I also tried making a Route Mixin with action so I can simply sendAction from the component with the selectedPage , but I don't know how to get the router (in order to call router.transitionTo ) within my Mixin. 我也尝试使用动作制作Route Mixin,所以我可以简单地从带有selectedPage的组件sendAction动作,但我不知道如何在我的Mixin中获取路由器(以便调用router.transitionTo )。

We should use public API for transitioning, that is provided in Route transitionTo and in Controller transitionToRoute . 我们应该使用公共API进行转换,它在Route transitionTo和Controller transitionToRoute中提供 this is the recommended approach. 这是推荐的方法。

  1. As you said, you can make it work in the component using Ember.getOwner(this).lookup('router:main').transitionTo('myRouteName') but that's strongly against good design. 正如你所说的,你可以使用Ember.getOwner(this).lookup('router:main').transitionTo('myRouteName')使它在组件中工作,但这很好地反对了良好的设计。
  2. You can define method named myTransitionToUrl inside actions hash of the Application route, and install ember-route-action-helper addon to call route action method myTransitionToUrl from component. 您可以在Application路由的actions哈希中定义名为myTransitionToUrl方法,并从组件中调用ember-route-action-helper addon来调用路由操作方法myTransitionToUrl
  3. If you don't want to define method in application route, then you can define it in mixin and implement it in the required route. 如果您不想在应用程序路径中定义方法,则可以在mixin定义它并在所需的路径中实现它。 that's also will work. 那也行。
  4. My choice and Recommended approach would be, Say pagination-component has prev , next , transtionTo actions, then I will implement all those actions in Mixin and extend it in all route which uses pagination-component . 我的选择和推荐的方法是,Say pagination-componentprevnexttranstionTo动作,然后我将在Mixin实现所有这些动作并在使用pagination-component所有路径中扩展它。 and will inform any data changes to route through actions from component. 并将通知任何数据更改以通过组件的操作进行路由。 that will ensure Data Down Actions Up strategy too. 这也将确保Data Down Actions Up策略。

  5. If you are just transitioning to a different route, then the Latest possible way is, 如果您只是转换到不同的路线,那么最新的可能方式是,

If ember version is greater than 2.15, then you can inject RouterService and and call transitionTo method directly. 如果ember版本大于2.15,那么你可以注入RouterService并直接调用transitionTo方法。 If you are using the ember version 2.15 or below, then you can use the router service polyfill to use RouterService. 如果您使用的是Ember版本2.15或更低版本,则可以使用路由器服务 polyfill来使用RouterService。

import Ember from 'ember';

export default Ember.Component.extend({
  router: Ember.inject.service(),

  actions: {
    next() {
      this.get('router').transitionTo('other.route');
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM