问题描述
afterRender
可用于模板绑定,但是将模板转换为组件后,似乎没有任何使用afterRender
的方法.我试图寻找一个使用afterRender
但找不到任何东西的组件的示例.
afterRender
works with template bindings, but after converting my templates to components, there does not seem to be any way to use afterRender
. I have tried looking for an example of a component that uses afterRender
, but cannot find anything.
推荐答案
我无法使上述方法正常工作.但是我在git问题列表上找到了一种解决方法,它不需要自定义的KO绑定.
I could not get the method working as per the above post. However i found a workaround on the git issue list and it doesn't require a custom KO Binding.
在组件模板html或代码字符串中添加以下行.
Add the below line in your component template html or string of code.
<span data-bind="template: { afterRender: init }"></span>
然后在您的模块/viewModel中创建一个初始化函数:
Then create a init function in your module / viewModel:
this.init = function() {
Do cool DOM stuff here.
}
或取决于您的viewModel结构:
or depending on your viewModel structure:
viewModel: function(params) {
return {
init: function () {
}
};
},
像魅力一样工作.工作示例在这里
Works like a charm. Example of it working is here
http://jsfiddle.net/gLcfxkv6/1/
在这里敲除git的线程: https://github.com/knockout/knockout/issues/1533
Thread on knockout git here:https://github.com/knockout/knockout/issues/1533
感谢git上的鞋面解决方法.
Thanks to vamps on git for the workaround.
这篇关于afterRender是否可与Knockout组件一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!