最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

I can't seem to get v-show and v-else to work. The documentation says:

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

Documentation: .html#v-show

Fiddle: /

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'
        }]
    }
});

It's probably something simple but I can't quite figure it out?

I can't seem to get v-show and v-else to work. The documentation says:

The v-else element must following 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'
        }]
    }
});

It's probably something simple but I can't quite figure it out?

Share Improve this question edited Oct 31, 2021 at 8:21 Penny Liu 17.4k5 gold badges86 silver badges108 bronze badges asked Nov 27, 2015 at 19:11 Ryan MortierRyan Mortier 8731 gold badge13 silver badges25 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

It looks like v-for and v-else don't work together well. I would place the condition higher up (on the <tbody>) and use v-if instead (that way, you wont have two <tbody> elements):

<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/

From the docs

A v-else element must immediately follow a v-if or a v-else-if element - otherwise it will not be recognized.

Also see this

Note that v-show doesn’t support the <template> syntax, nor does it work with v-else.

发布评论

评论列表(0)

  1. 暂无评论