Vue.js v-show and v-else not working as intended?

I cannot get v-show and v-else to work. The documentation states:

The v-else element must follow immediately after the v-if or v-show element, otherwise it will not be recognized.

Documentation : http://vuejs.org/guide/conditional.html#v-show

Fiddle : https://jsfiddle.net/p2ycjk26/2/

Html:

<table>
    <thead>
        <tr>
            <th>Heading</th>
        </tr>
    </thead>
    <tbody>
        <tr v-for="test in tests" v-show="tests.length">
            <td>{{ test.name }}</td>
        </tr>
        <tr v-else>
            <td>No data available in table</td>
        </tr>
    </tbody>
</table>

JavaScript:

new Vue({
    el: 'table',

    data: {
        tests: [{
            name: 'Testing'
        }, {
            name: 'Testing 2'
        }, {
            name: 'Testing 3'
        }]
    }
});

Something may be simple, but I cannot understand it.

+4
source share
2 answers

It looks like v-forand v-elsedoes not work well. I would put the condition above (on <tbody>) and use it instead v-if(this way you won't have two elements <tbody>):

<table>
    <thead>
        <tr>
            <th>Heading</th>
        </tr>
    </thead>
    <tbody v-if="tests.length">
        <tr v-for="test in tests">
            <td>{{ test.name }}</td>
        </tr>
    </tbody>
    <tbody v-else>
        <tr>
            <td>No data available in table</td>
        </tr>
    </tbody>
</table>

https://jsfiddle.net/p2ycjk26/4/

+4

docs

v-else v-if v-else-if, .

. this

, v-show <template> v-else.

+1

Source: https://habr.com/ru/post/1617778/


All Articles