简体   繁体   English

将嵌套的Knockout.js组件与RequireJs一起使用

[英]Using nested Knockout.js components with RequireJs

I've a Knockout.js component that uses a nested pagination component like this: 我有一个Knockout.js组件,它使用嵌套的pagination组件,如下所示:

TypeScript code TypeScript代码

import ko = require("knockout");

class NetworkListViewModel {
    arrayValue = ko.observableArray<string>();
    currentPage = ko.observable<number>(0);
    totalItemCount = ko.observable<number>(0);

    constructor(params) {
        this.arrayValue = params.value
        this.currentPage(1);
        this.totalItemCount(params.totalItemCount);
    }
}

ko.components.register("network-list", {
    viewModel: NetworkListViewModel,
    template: { require: "text!components/network-list/network-list.html" }
});

HTML View HTML视图

<div class="network-list" >
    <pagination params="CurrentPage: CurrentPage, TotalItemCount: TotalItemCount()"></pagination>
</div>

I've tryied to add a script to register it in RequireJs in this way: 我试图添加一个脚本以这种方式在RequireJs中注册它:

HTML View HTML视图

<div class="network-list" >
    <pagination params="CurrentPage: CurrentPage, TotalItemCount: TotalItemCount()"></pagination>
</div>
<script>
    require(["components/pagination/pagination"]);
</script>

but it doesn't works. 但它不起作用。

Registering those dependencies in the main view (index.cshtml) it works. 在主视图(index.cshtml)中注册那些依赖项即可。

<div id="bindingSection">
    <network-list params="value: data, totalItemCount: totalItemCount()">  </network-list>
</div>

@section Scripts {
<script>
    require(['pages/home',
        "components/network-list/network-list",
        "components/pagination/pagination"
    ]);
</script>
}

Doing this implies that the developer have to know all child dependencies of each component (not simple in large projects). 这样做意味着开发人员必须知道每个组件的所有子依赖关系(在大型项目中这并不简单)。 How can I instruct RequireJs to manage this nested component? 如何指示RequireJs管理此嵌套组件? Is there a way to move import responsability to the child component? 有没有办法将进口责任转移到子组件?

The component for pagination is required for the network list, so the appropriate place to put the require is in the script for the networklist: 网络列表需要分页组件,因此在网络列表的脚本中放置需求的适当位置:

import ko = require("knockout");
import pagination = require("components/pagination/pagination");

class NetworkListViewModel {
   // code..

There is an issue that the TypeScript compiler will 'ignore' a require() statement if you don't actually reference the import in the code. 如果您实际上未在代码中引用导入,则TypeScript编译器会“忽略” require()语句,这是一个问题。 You can work around this by adding a dummy line, eg var tmp = pagination; 您可以通过添加虚拟行来解决此问题,例如var tmp = pagination; , or using the alternative import syntax, eg ,或使用其他导入语法,例如

/// <amd-dependency path="components/pagination/pagination" />

import ko = require("knockout");

class NetworkListViewModel {
   // code..

This approach will force TypeScript to load the module. 这种方法将强制TypeScript加载模块。

I use this sort of dependency a lot and it works very well. 我经常使用这种依赖关系,并且效果很好。 Often you only have a single require() in the page for the view model, all the rest is inherited. 通常,视图模型的页面中只有一个require() ,其余的都是继承的。

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

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