afterRender 是否适用于 Knockout 组件? [英] Does afterRender work with Knockout components?

查看:22
本文介绍了afterRender 是否适用于 Knockout 组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 中创建一个 init 函数:

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

感谢 vamps on git 的解决方法.

Thanks to vamps on git for the workaround.

这篇关于afterRender 是否适用于 Knockout 组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆