Conditionals and Loops
Conditional Rendering
To render a widget or container only under certain conditions, you can use the v-if
directive:
In this example, both Text widgets are displayed because the condition evaluates to true. When the value of the seen
property is changed to false, the second Text widget is removed from the window.
You can use v-else-if
and v-else
directives to create conditions with multiple alternatives:
Note that conditional directives destroy and create the widgets whenever the condition changes. To temporarily hide a widget without destroying it, you can use the visible attribute.
See also Conditional Rendering in the Vue.js documentation for more details.
List Rendering
To render multiple instances of the same component, you can use the v-for
directive:
In this example, three Text widgets are rendered, one for each element of the items
array. When an item is added or removed from this array, corresponding Text widgets are automatically added or removed.
It's recommended to provide a unique key
attribute for each item. This way, the state of widgets will be preserved when array items are reordered.
See also List Rendering in the Vue.js documentation for more details.
Last updated