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

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

","image":"http://www.luping.net/uploads/20241022/17295566466716f0a64994b.png","datePublished":"2024-11-01T01:01:07+08:00","dateModified":"2024-11-01T01:01:07+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Generating dynamic layouts in Vue using recursive components

Generating dynamic layouts in Vue using recursive components

Published on 2024-11-01
Browse:452

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


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



Then, we could import it and run the parser.


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


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.







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







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

Release Statement This article is reproduced at: https://dev.to/waruna/generating-dynamic-layouts-in-vue-using-recursive-components-4ci7?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3