Dynamic Layout with a useLayout Composable
Explore how to create and manage dynamic page layouts in Vue using the Composition API through a useLayout composable and a layout factory function. Understand how to write concise, reusable, and stateful logic for switching layouts efficiently and maintaining clean component code.
We'll cover the following...
Manage layout using Composition API
Composition API is another way in which we can manage layouts. We’ll create a useLayout composable that works in a similar way to the pageLayoutService, but the code will be much more succinct, as we’ll see in a moment. Due to how the Composition API works, we won’t need the layoutComputed. Instead, we only need to define a reactive value with the ref and provide the useLayout function.
As in the previous example, we also have to update the Layout and LayoutExample components.
In the LayoutExample component, we update the script like this:
The updated script for the Layout component is:
As mentioned before, the useLayout.js requires less code than the layoutService, and consuming its values and methods is much cleaner, as we don’t have to spread layoutComputed, nor assign LAYOUTS on the instance in the created hook. We initialize ...