【问题标题】:Durandal: Create function in one child and Consume from another childDurandal:在一个孩子中创建功能并从另一个孩子中消费
【发布时间】:2018-12-16 18:39:58
【问题描述】:

我有一个使用Durandal.JS 开发的Knockout 应用程序。这是我的项目的结构:

- Parent
    - Index.js
    - Index.html
- Child1
    - Index.js
    - Index.html
- Child2
    - Index.js
    - Index.html

我在Child2/Index.js 中声明了一个函数UpdateData(),我想从Child1/Index.js 使用该函数

Child2 中声明此函数的原因是,我在 Child2 中声明了其他变量和控件。并且通过调用UpdateData()方法,我想更新Child2中声明的所有变量的值。

我不知道如何处理这种情况。

【问题讨论】:

标签: knockout.js durandal durandal-2.0


【解决方案1】:

Durandal 默认使用 require 来加载模块。您可以尝试在您的 child1 模块中要求 child2 模块。

//child1 module
define(function (require) {

//require child2 into the module
var child2 = require('Child2/Index');

//use the method
child2.UpdateData();
});

为此,您必须从Child2 返回UpdateData() 方法。

//child2 module
define(function(){
return {
UpdateData:function(){
  //your method
}
}});

有关更多信息,您可以阅读有关如何创建可被其他模块重用的模块的文档。 http://durandaljs.com/documentation/Creating-A-Module.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多