简体   繁体   English

卸载聚合物页面

[英]Unloading Polymer pages

I've been POCing Polymer in combination with Redux and the Polymer (2.0) starter kit, which amongst other things introduces the iron-pages component, as well as the lazy loading of the pages contained therein. 我一直在与Redux和Polymer(2.0)入门套件结合使用POCing Polymer,该套件除其他外介绍了iron-pages组件以及其中包含的页面的延迟加载。

However, I've noticed that they're not actually pages as such, but more like an old-fashioned tab bar , where all content is loaded into the DOM but hidden using CSS. 但是,我注意到它们实际上并不是页面,而是更像老式的标签栏 ,其中所有内容均已加载到DOM中,但使用CSS隐藏。 The connectedCallback() lifecycle method is called, but the disconnectedCallback() is not because the component is never unloaded. 调用了connectedCallback()生命周期方法,但并不是disconnectedCallback()并不是因为组件从未卸载过。

This feels like a memory and performance leak to me. 这感觉就像是对我的记忆和性能泄漏。 One use case we will implement is a frequently updating piece of data through websockets; 我们将实现的一个用例是通过websocket频繁更新数据。 what I want to avoid is it updating in the background. 我要避免的是它在后台更新。 For desktop I guess it would be okayish, but for mobile it'd be terrible for performance. 对于台式机,我想它可以,但是对于移动设备,它的性能将很糟糕。

Second, I'd rather not have to implement manual lifecycle management; 其次,我宁愿不必实施手动生命周期管理; smells of NIH. NIH的气味。

So, what it boils down to: Is there either component unloading, or reliable lifecycle callbacks in Polymer and/or a paging web component? 因此,可以归结为:在Polymer和/或分页Web组件中是否存在组件卸载或可靠的生命周期回调?

edit: I see Unloading Polymer pages has the same question - it's unanswered though. 编辑:我看到“ 卸载聚合物”页面有相同的问题-虽然没有答案。

edit 2: I see there was an issue raised about this on iron-pages too. 编辑2:我也看到在铁页上也存在与此相关的问题。

As the related question says, you can use a conditional template ( <template is="dom-if"> ) with the restamp attribute. 如相关问题所述,您可以将条件模板( <template is="dom-if"> )与restamp属性一起使用。 With restamp , the elements are removed when the conditional is false, so your disconnectedCallback is called. 随着restamp ,元素被移除时的条件是假的,所以你的disconnectedCallback被调用。

You could use a set of dom-if templates instead of the iron-pages element. 您可以使用一组dom-if模板来代替iron-pages元素。

As you noted, the iron-pages element is more like a tab panel, and works well when the underlying views are comparatively light, or you expect people to switch back and forth frequently and don't want to pay the cost of recreating them. 如您所述, iron-pages元素更像是一个选项卡面板,并且在基础视图相对较浅时效果很好,或者您希望人们经常来回切换并且不想支付重新创建它们的成本。 However in many cases when the elements take a lot of memory or (as in your case) are doing work as long as they're on the page, a conditional template is the way to go. 但是,在许多情况下,当元素占用大量内存或(只要您在您的情况下)只要它们在页面上就可以工作时,有条件的模板就是解决方法。

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

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