当使用 IoC 容器时,我可以指定当需要给定的依赖项时,要么每次传入一个新对象,要么传入的对象始终是同一个实例。
我曾认为内部和外部模块分别提供了该功能,但现在我不太确定。此外,我发现在使用“内部”模块时,Knockout 中的自定义绑定(bind)会收到“未定义”作为 viewModel
参数。
如何使用外部模块在单例和 transient 之间进行选择?
我在 TypeScript 中使用 Durandal(Knockout、RequireJS)。
最佳答案
"undefined" as the viewModel argument when using an "internal" module
这是一个与 JavaScript 发出顺序有关的常见错误。更多相关信息:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md
When using an IoC container, I can specify that when a given dependency is needed, that either a new object is passed-in each time, or that the object passed-in is always the same instance.
外部模块系统与 IoC 容器不同。模块是一个单例,并且您总是获得相同的实例。如果您想要拥有不同的实例,请让您的模块导出一个函数(它可以充当您实际想要的对象的工厂)。
关于javascript - knockout 内部/外部模块!==短暂/单一的生活方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32727472/