简体   繁体   English

防止 Vue.js 重新渲染子组件

[英]Prevent Vue.js re-rendering child components

I've got a complex component which does all its rendering in a render function.我有一个复杂的组件,它在render函数中完成所有渲染。 There are multiple parts to this, and different bits of the view get rendered - one of these things is a filter bar, showing the filters that have been applied.这有多个部分,视图的不同部分被渲染 - 其中之一是过滤器栏,显示已应用的过滤器。

What I'm noticing happening, is if I apply a filter which in turn presents this bar, it causes everything else to be fully re-rendered.我注意到发生的事情是,如果我应用一个过滤器,它反过来呈现这个栏,它会导致其他一切都被完全重新渲染。 This is causing a number of other issues and I need to try and stop it from happening.这导致了许多其他问题,我需要尝试阻止它发生。

I've never come across this issue when using normal templates as Vue seems to handle these very intelligently, but I have no idea how to tackle this.我在使用普通模板时从未遇到过这个问题,因为 Vue 似乎非常聪明地处理这些问题,但我不知道如何解决这个问题。 The only thing I can think of is setting a key on each thing I don't want re-rendered but not sure if this will a) solve the problem, and b) be possible for the content that is passed in through a slot我唯一能想到的就是在我不想重新渲染的每件事上设置一个key ,但不确定这是否会 a) 解决问题,并且 b) 可以为通过插槽传入的内容

Has anyone else faced this issue, and if so how can it be solved?有没有其他人遇到过这个问题,如果有,如何解决?

I had a similar issue when using vuetify text inputs in a complex component which was causing the app to slow down drastically.在复杂的组件中使用 vuetify 文本输入时,我遇到了类似的问题,这导致应用程序急剧变慢。

In my search I found this link which was specific to vuetify:在我的搜索中,我找到了这个特定于 vuetify 的链接:

high performance impact when using a lot of v-text-field使用大量 v-text-field 时的高性能影响

then found out that this is actually a vue thing from this GitHub issue:然后从这个 GitHub 问题中发现这实际上是一个 vue 的东西:

Component with slot re-renders even if the slot or component data has not changed即使插槽或组件数据没有改变,带有插槽的组件也会重新渲染

and there is plan to improve this in it is tracked here (vue 3 should resolve this issue):并且有计划改进这一点,在这里跟踪(vue 3应该解决这个问题):

Update slot content without re-rendering rest of component更新插槽内容而不重新渲染组件的其余部分

so after reading through all these I found some workarounds that helped me a lot to boost the performance of my app, I hope these will help you as well:因此,在阅读完所有这些内容后,我发现了一些对提高应用程序性能有很大帮助的解决方法,我希望这些方法也能对您有所帮助:

  1. divide that complex component into smaller ones specially when there is some bit of code that changes data that bounds to template causing re-rendering (put them in their own component)将复杂的组件分成较小的组件,特别是当有一些代码更改绑定到模板的数据导致重新渲染时(将它们放在自己的组件中)

  2. I moved all data layer control to the vuex store, instead of using v-model every where and passing data as events and props, all the data is updating in the store through an action and read from the store through a getter.我将所有数据层控制移至 vuex 存储,而不是在任何地方使用v-model并将数据作为事件和道具传递,所有数据都通过操作在存储中更新,并通过 getter 从存储中读取。 (from data I mean somethings that is being looped in the template in a v-for , API results, and so on... all of them is being set, updated and read through the store. my components still have the data object but only for the things related to the style and template control like a boolean to control a modal or an imported icon which is used in the template and alikes) (从数据中,我的意思是在v-for模板中循环的东西,API 结果,等等......所有这些都被设置、更新和读取通过商店。我的组件仍然有数据对象,但仅适用于与样式和模板控件相关的事物,例如布尔值来控制模板中使用的模态或导入的图标等)

  3. lastly I wrote a function called lazyCaller which its job is to update the values in the store with a delay (when immediate data update isn't necessary) to avoid rapid updates comping from something like a text input (with out this every key stroke trigger the value update action)最后,我编写了一个名为lazyCaller的函数,它的工作是延迟更新存储中的值(当不需要立即更新数据时)以避免快速更新来自文本输入之类的东西(没有这个每个按键触发值更新操作)

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

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