dynamic-vue-layout-components icon indicating copy to clipboard operation
dynamic-vue-layout-components copied to clipboard

Pages are rendered twice, when changing layouts

Open dobromir-hristov opened this issue 6 years ago • 8 comments
trafficstars

Issue

When swapping layouts, pages are getting re-rendered twice. This could be an issue for those using mounted or created hooks to fetch some data, as they get called two times. Did not test if all children also get re-rendered.

How to reproduce

  1. Dedicate different layouts, for different routes.
  2. Go from one route to the Other

What happens?

The route gets rendered, its layout component emits an event to change the layout, the layout gets replaced, which triggers a render on the page again.

Thank you for the great article, opened my eyes on some patterns a bit. Been scratching my head with this the past few days. I tried with assigning keys and scoped slots and what knot, but no dice..

dobromir-hristov avatar Aug 05 '19 11:08 dobromir-hristov

Thank you for your feedback. I have this problem on my todo list. I'll look into it when I find the time. If you find a solution I'd be very happy if you'd share it with us. Thx!

maoberlehner avatar Aug 05 '19 11:08 maoberlehner

Thanks for replying so fast :)

As I said, been trying out some things, but with no success. Only thing I could think of, was to use the router guards instead of lifecycle hooks, but then you loose that flexibility of nested layouts and the simplicity of the template based approach. It could lead to other issues I guess too, like reverting the layout if deny navigation in a later router guard.

I am not exactly sure how the Vue diffing works for components, as in to stop if from re-rendering children when the dynamic layout is swapped. Not sure who to ask as well :/

dobromir-hristov avatar Aug 05 '19 11:08 dobromir-hristov

Having the same issue! Let me know if you come up with a solution

lynnmugambi avatar Aug 16 '19 16:08 lynnmugambi

Just noticed this as well. The sequence seems to be:

  • component created
  • component mounted
  • layout created
  • component created again
  • component mounted again
  • layout mounted

From a code perspective, the approach makes sense, but I don't like that Vue is re-creating components like this.

namero999 avatar Jan 03 '20 11:01 namero999

I have found a satisfying solution. Would a PR be the best approach to share it with you guys?

namero999 avatar Jan 03 '20 13:01 namero999

Please share :)

dobromir-hristov avatar Jan 03 '20 13:01 dobromir-hristov

You can check the solution here -> https://github.com/maoberlehner/dynamic-vue-layout-components/pull/4

namero999 avatar Jan 03 '20 13:01 namero999

@namero999 : Thank you very much for this elegant solution. It feels more natural to me

piavgh avatar Nov 30 '20 02:11 piavgh