vue2 中 v-for 不能嵌套使用 v-if 吗?

2017-08-29 14:00:36 +08:00
 yuann72

v-for 中嵌套使用 v-if 报错
是我哪里写错了吗?

<body>
    <div id="test">
        <p v-for="d in hhh">
            {{  d  }}
            {{  d.type  }}
            {{  d.msg  }}
            <h1 v-if="   d.type == 'h1'   "> {{ d.msg }} </h1>
            <h2 v-if="   d.type == 'h2'   "> {{ d.msg }} </h2>
            <h3 v-if="   d.type == 'h3'   "> {{ d.msg }} </h3>
        </p>
    </div>
</body>
<script src="../vue.js"></script>
<script >
   let vm= new Vue({
        el:"#test",
        data:{
            hhh:[
                {
                    'type':'h1',
                    'msg':'h1h1h1'
                },
                {
                    'type':'h2',
                    'msg':'h2h2h2'
                },
                {
                    'type':'h3',
                    'msg':'h3h3h3'
                },
            ]
        },
    });
</script>

下面是浏览器的报错信息:

[Vue warn]: Property or method "d" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

[Vue warn]: Error in render function: "TypeError: Cannot read property 'type' of undefined"
4182 次点击
所在节点    问与答
4 条回复
U2Fsd
2017-08-29 14:17:34 +08:00
xiaome
2017-08-29 14:18:07 +08:00
当它们处于同一节点,v-for 的优先级比 v-if 更高
https://cn.vuejs.org/v2/guide/list.html#v-for-with-v-if
yuann72
2017-08-29 14:27:30 +08:00
@xiaome v-for 和 v-if 并不是同级别啊, v-if 在 v-for 下一级
yuann72
2017-08-29 14:28:20 +08:00
@U2Fsd 懂了,谢了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/386600

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX