Created
April 15, 2020 13:32
-
-
Save andreasvirkus/37664b49871e5b09eaff4adf965cb34b to your computer and use it in GitHub Desktop.
Demonstrating recursive Vue components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div id="app"> | |
<nested-list | |
v-for="item in list" | |
:key="item.name" | |
:name="item.name" | |
:items="item.items" | |
:level="0" | |
/> | |
</div> | |
</template> | |
<script> | |
import NestedList from './NestedList' | |
export default { | |
name: 'MasterList', | |
components: { NestedList }, | |
data() { | |
return { | |
list: [ | |
{ | |
name: 'master on level 0', | |
items: [ | |
{ | |
name: 'item on level 1', | |
items: [ | |
{ | |
name: 'item on level 2', | |
items: [{ name: 'item on level 3' }], | |
}, | |
{ name: 'item on level 2' }, | |
{ name: 'item on level 2' }, | |
], | |
}, | |
{ name: 'item on level 1' }, | |
], | |
}, | |
], | |
} | |
}, | |
} | |
</script> | |
<style> | |
body { | |
background: #20262e; | |
padding: 20px; | |
font-family: Helvetica; | |
} | |
#app { | |
background: #fff; | |
border-radius: 4px; | |
padding: 20px; | |
transition: all 0.2s; | |
} | |
.item { | |
position: relative; | |
} | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<div class="item" :style="indentation">{{ name }}</div> | |
<nested-list | |
v-for="item in items" | |
:key="item.name" | |
:name="item.name" | |
:items="item.items" | |
:level="level + 1" | |
/> | |
</div> | |
</template> | |
<script> | |
export default { | |
name: 'NestedList', | |
props: { | |
name: String, | |
level: Number, | |
items: Array, | |
}, | |
computed: { | |
indentation() { | |
return { transform: `translateX(${this.level * 30}px)` } | |
}, | |
}, | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment