【问题标题】:What is the proper way to invoke a closure action inside a component?在组件内调用闭包操作的正确方法是什么?
【发布时间】:2018-07-24 07:14:39
【问题描述】:

我像这样将关闭操作传递给我的组件:

{{deployment-timeline loadMoreDeployments=(action "loadMoreDeployments")}}

我应该如何在我的组件中调用它?

actions:{
  loadMoreDeployments(){
    // which one of the following three invocations is best?
    this.attrs.loadMoreDeployments();
    this.get('loadMoreDeployments')();
    this.loadMoreDeployments();
  }
}

【问题讨论】:

    标签: javascript ember.js ember-components


    【解决方案1】:

    你应该这样做

    this.get('loadMoreDeployments')();
    

    import {get} from '@ember/object';
    ...
    get(this, 'loadMoreDeployments')();
    

    attrs 的使用可能从未被引入,并且不建议使用点符号访问 ember 对象上的属性。

    编辑:With ember 3.1 (currently in beta) we will get support for dot notation for getters (no setters yet, and not for proxy objects). 这意味着从 ember 3.1 开始,您可以安全地使用:

    this.loadMoreDeployments();
    

    【讨论】:

    • 我“得到”了新语法——但我们将失去更多的采用。 : (
    • IS 很酷......但它也是 第三种方式 - 有例外。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2017-09-13
    • 2011-11-19
    • 1970-01-01
    • 2021-08-23
    • 2012-10-15
    • 1970-01-01
    相关资源
    最近更新 更多