P粉6114563092023-08-26 09:17:09
Because Vue will never know if the final result has changed before recalculating. Therefore, the calculated variable will be recalculated every time its dependencies change, which is unavoidable.
A common misunderstanding is that Vue caches calculated variables by calculating the value of the variable, but in fact, Vue caches calculated variables by the state of its dependencies.
You can create an observer (as you know) or wrap the template using the calculated value into the component.
Why wrap it into another component help here?
Because Vue will convert your template into a rendering function. The function is recalculated every time its dependencies change. Sound familiar? Yes, it works like a calculated variable. The dependencies of the render function are all the variables you use in the template. So if you wrap frequently changing variables into a component, Vue will only re-render that component and avoid re-rendering your large component. This will have a big impact on your performance