简体   繁体   English

通过require.js加载所有内容时,如何将Knockout视图模型的一部分传递给组件?

[英]How do I pass parts of my Knockout viewmodel to a component when loading everything via require.js?

I'm getting started with Knockout.js components. 我开始使用Knockout.js组件。 I need to pass a component a part of my viewmodel as its data to use. 我需要将组件传递给我的视图模型的一部分作为要使用的数据。

When the viewmodel is global, this is simple 当视图模型是全局的时,这很简单

<my-component params="myGlobalViewModel.dataSubSet"></my-component>

How do I do this when the viewmodel is loaded via require.js, and there is no access to this from global scope? 当通过require.js加载视图模型并且无法从全局范围访问该视图模型时,该怎么办?

At the moment I additionally store a reference to the viewmodel in a global variable and then use this. 目前,我还在一个全局变量中存储了对视图模型的引用,然后使用它。 Is there a better way? 有没有更好的办法?

Your viewmodel is made "global" (from the HTML bindings' perspective) when you call ko.applyBindings() on it. 当您在其上调用ko.applyBindings() ,其视图模型将ko.applyBindings() “全局”(从HTML绑定的角度来看)。 If dataSubSet is a member of your viewmodel, just do 如果dataSubSet是您的视图dataSubSet的成员,则只需执行

<my-component params="dataSubSet"></my-component>

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

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