【问题标题】:How to communicate between ractive components?如何在反应组件之间进行通信?
【发布时间】:2016-08-13 22:45:11
【问题描述】:

我遇到了一个问题,我想在不同的 ractIve 组件中调用函数。 例如,我有 2 个活性组件 A 和 B,我想从另一个活性组件 B 调用 A.xyz() fintion。

【问题讨论】:

  • 视图层次结构中的组件之间的关系是什么?他们是否处于祖先/后代关系?还是他们是兄弟姐妹?
  • 根本没有关系。组件 A 和 B 处于不同的层次结构中。

标签: components ractivejs


【解决方案1】:

我们可以为此使用全局 Ractive 实例。 来自全局 Ractive 实例上的组件 B 触发事件。

Ractive.default.fire('call.componentA.event')

在组件 B 上捕获此事件

Ractive.default.on('call.componentA.event', function (){
//Do your stuff here
})

【讨论】:

    【解决方案2】:

    您可以尝试在Ractive root 上触发消息

    组件 A.js

    export default Ractive.extend({
    
      template,
    
      oninit () {
        ...
        var obj = {// some data to be sent to component B};
    
        this.root.fire('eventType', obj);
      }
    
    });
    

    在组件 B “接收”

    componentB.js

    export default Ractive.extend({
    
      template, // template for 
    
    
      oninit () {
    
        this.root.on('eventType', ( obj ) => {
             // ...do something with the object
        });
      }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2016-05-29
      • 2019-07-26
      • 2016-08-10
      • 2013-10-02
      相关资源
      最近更新 更多