来自组件内部的transitionToRoute('route') [英] transitionToRoute('route') From Inside Component

查看:81
本文介绍了来自组件内部的transitionToRoute('route')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从组件操作内部实用地转换为路线?

我尝试使用 @ get('controller')。transitionToRoute('images'),但控制器引用组件本身。我知道组件应该是独立的,所以我应该使用视图代替它与控制器/路由进行更好的交互吗?

I tried to use @get('controller').transitionToRoute('images'), but the controller refers to the component itself. I understand that components should be self contained, so should I be using a view instead to interact with controllers/routes better?

示例

App.ImageEditorComponent = Ember.Component.extend
  ...
  actions:
    delete: ->
      App.Files.removeObject(object)
      @transitionToRoute('images') # This throws an exception
  ...


推荐答案

在父控制器上创建操作。

Create action on parent controller.

export default Ember.Controller.extend({
  actions: {
    transInController() {
       this.transitionToRoute('home')
    }
  }
});

在组件调用中指定此操作。

Specify this action on component call.

{{some-component transInComponent=(action "transInController")}}

之后 v3。 4.0(2018年8月27日)

AFTER v3.4.0 (August 27, 2018)

some-component.js

some-component.js

export default Component.extend({
  actions: {
      componentAction1() {
          this.transInComponent();
      }
  }
});

OR 在某些组件中更简单。 hbs

OR simpler in some-component.hbs

<button onclick={{@transInComponent}}>GO HOME</button>

v3.4.0之前的版本

Ember.component.sendAction

从组件到控制器的发送操作

"Send Action" from component up to controller

export default Ember.Component.extend({
  actions: {
      componentAction1() {
          this.sendAction('transInComponent');
      }
  }
});

这篇关于来自组件内部的transitionToRoute('route')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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