简体   繁体   English

bootstrap affix插件内存泄漏

[英]bootstrap affix plugin memory leak

These lines in the bootstrap affix plugin seem to cause a memory leak because the window gets a reference to the affix instance that's never released. 引导程序附加插件中的这些行似乎会导致内存泄漏,因为窗口会获取对从未发布的附加实例的引用。

As a workaround I'm using this code to release the references when removing the affixed element from the DOM: 作为一种解决方法,我在从DOM中删除附加元素时使用此代码来释放引用:

$(window)
    .off('scroll.bs.affix.data-api')
    .off('click.bs.affix.data-api');

Seems kind of hacky- is there a better way of doing this? 看起来有点hacky-有更好的方法吗? Didn't see anything in the affix plugin docs . affix插件文档中没有看到任何内容。

By default, bootstrap Affix listens for scroll and click events on $(window) using the .bs , .affix , and .data-api namespaces. 默认情况下,引导程序Affix使用.bs.affix.data-api命名空间侦听$(window)上的scrollclick事件。

$.off('.affix'); will remove all listeners in the .affix namespace. 将删除.affix命名空间中的所有侦听器。 $(window).off('.affix'); will remove all listeners in the .affix namespace from the window element. 将从window元素中删除.affix命名空间中的所有侦听器。 If you only have one Affix, and are affixing it to the window, it has the exact same effect as $.off('.affix'); 如果你只有一个词缀,并且正在将它粘贴到窗口,它与$.off('.affix');具有完全相同的效果$.off('.affix');

Adding in the other namespaces makes it more specific, but unless you are using the .affix namespace in your own code, the added specificity doesn't change anything. 添加其他名称空间使其更具体,但除非您在自己的代码中使用.affix名称空间,否则添加的特异性不会改变任何内容。 You don't want to remove the other namespaces independently of .affix if you are using any other bootstrap elements. 如果您使用任何其他引导程序元素,则不希望独立于.affix删除其他名称空间。

$('.affix').off('.affix'); will not work because the listeners are not on the Affixed element, but on the target that element is Affixed to , ie the window. 无法工作,因为侦听器不在Affixed元素上,而是在该元素附加的目标 ,即窗口。

pstenstrm is correct that there is no way to detect that an element is removed from the DOM, or injected for that matter. pstenstrm是正确的,没有办法检测到从DOM中删除了一个元素,或者为此注入了元素。 So if the code later re-injects the element, and you want to behave as an Affix again, you'll need to use the bootstrap JS api to call Affix again. 因此,如果代码稍后重新注入元素,并且您希望再次表现为Affix,则需要使用引导程序JS api再次调用Affix。

I took @Carrie Kendall's recommendation and opened a bug report... well commented on a related bug report. 我接受了@Carrie Kendall的推荐并开了一个错误报告......很好地评论了相关的错误报告。

https://github.com/twbs/bootstrap/issues/13655 https://github.com/twbs/bootstrap/issues/13655

What we need in this case is a "destroy" method for the affix plugin and some documentation on the getbootstrap site so that people using the affix plugin in single page apps can avoid the memory leak pitfall when removing their affixed content. 在这种情况下我们需要的是一个affix插件的“destroy”方法和getbootstrap网站上的一些文档,以便在单页应用程序中使用affix插件的人可以在删除附加内容时避免内存泄漏陷阱。

There is no way to detect when an element is removed from the DOM. 无法检测何时从DOM中删除元素。 The affix plugin can't automatically remove the listeners. affix插件无法自动删除侦听器。 The way you do it is the right way. 你这样做是正确的方式。

Though by calling $(window).off() you remove every listener, even those you might want to keep. 虽然通过调用$(window).off()可以删除每个侦听器,甚至是那些你想要保留的侦听器。 It would be safer to only call $.off() on the element you are removing. 仅在要删除的元素上调用$.off()会更安全。

$('.affix').off('.affix');

The .bs , .affix and .data-api after the event name are namespaces. 事件名称后面的.bs.affix.data-api是名称空间。 By calling $.off('.affix') you remove every event declared in that namespace. 通过调用$.off('.affix')您可以删除该命名空间中声明的每个事件。 Which is probably the only better of doing what you're doing. 做你正在做的事情可能是唯一更好的做法。

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

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