Home > Web Front-end > JS Tutorial > body text

Generating dynamic layouts in Vue using recursive components

Susan Sarandon
Release: 2024-10-05 06:20:02
Original
127 people have browsed it

Inspiration for this article comes from a recent implementation I did to build dynamic layouts on Vue based front end.

Let's assume your API endpoint returns a YAML layout like below.


---
row:
  - col: 12
    row:
      - col: 12
        component_id: 1
  - col: 12
    row:
      - col: 6
        component_id: 2
      - col: 6
        row:
          - col: 12
            component_id: 3
          - col: 12
            component_id: 4


Copy after login

We would expect a layout like the below from this YAML expression.

Generating dynamic layouts in Vue using recursive components

So, it make this easy to work with we will parse this YAML structure and generate JSON object. I used yaml package.


npm install yaml



Copy after login

Then, we could import it and run the parser.


import YAML from 'yaml'
const jsonObject = YAML.parse(yaml_struct)


Copy after login

To make this work, we need to create a recursive Vue template and a component that can call itself recursively when encountering nested rows. This way, the structure will dynamically handle deeply nested layouts like the one I provided.


<template>
    <v-row>
        <template v-for="col in row">
            <v-col :cols="col.col">
                <template v-if="col.component_id">
                    <ComponentWantToRender :col="col"></ComponentWantToRender>
                </template>

                <template v-if="col.row">
                    <RecursiveRow :row="col.row"></RecursiveRow>
                </template>
            </v-col>
        </template>
    </v-row>
</template>

<script setup>
import { defineProps } from 'vue';
import RecursiveRow from './RecursiveRow.vue';
defineProps({
    row: Array,
});
</script>


Copy after login

Now, we can use this RecursiveRow component inside your parent component to handle the top-level layout.


<template>
  <RecursiveRow :row="jsonObject"></RecursiveRow>
</template>

<script setup>
import RecursiveRow from './RecursiveRow.vue';

defineProps({
  jsonObject: Array,
});
</script>


Copy after login

That's it. If you have any questions, please leave a comment below.

The above is the detailed content of Generating dynamic layouts in Vue using recursive components. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!