根据ko关于组件生命周期的组件文档:
如果组件绑定的名称值发生明显变化,或者如果封闭的控制流绑定导致容器元素被移除,则视图模型上的任何 dispose 函数都会在容器元素从 DOM 中移除之前被调用
我不确定为什么我的组件被放置在这个小提琴上。
<div data-bind='component: { name: "some-component", params: foo }'>
<p data-bind="text: name"></p>
</div>
function ComponentViewModel(params) {
}
ComponentViewModel.prototype.dispose = function() {
console.log('disposing...');
};
ko.components.register('some-component', {
viewModel: ComponentViewModel,
template : '<div></div>'
});
var rootvm = {
foo : ko.observable('1')
};
ko.applyBindings(rootvm);
setTimeout(function() {
rootvm.foo('2'); // this is disposing ComponentViewModel, why ??
}, 3000);
我在我的小提琴上出现的文档中看不到上述任何一点。如果注入的变化,我当然不希望出现component
。disposed
re-instantiated
params
任何想法为什么会发生这种情况?