A
A
Andrej Sharapov2019-10-04 10:00:37
Vue.js
Andrej Sharapov, 2019-10-04 10:00:37

How to correctly write v-if directives in a template?

How to correctly write v-if directives in the template, provided that the data structure can be either a string or an array? those. in Data { return { ... can be like this "code": "one",or like this "code": ["one", "two"],
In the template I write like this:

template: `
<template v-for="(value, index) in cd.code" :key="index">
    <small class="tag">{{ value }}</small>
</template>
`

works fine, but only for an array. If the code looks like this "code": "one",, then "one" is split into 3 tagssmall

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2019-10-04
@Madeas

If not an array, convert to an array:
or make it a computed property:

computed: {
  codeArray() {
    return Array.isArray(cd.code) ? cd.code : [cd.code];
  }
}

v-for="(value, index) in codeArray"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question