您的位置:首页 > Web前端 > Vue.js

Vue - ElementUI中循环渲染表格,控制字段的显示与隐藏 v-if与v-for同时使用

2019-06-21 10:03 2935 查看

在Vue中使用

v-for
循环一个数组/对象时,如果再使用
v-if
,那么会提示使用计算属性(能正常使用),因为Vue中是不提倡
v-for
v-if
同时使用的。
在我的项目中也遇到了问题
不过翻看文档解决了
修改前:

<el-table-column
v-for="(item, index) in columns"
:prop="item.prop"
:key="index"
align="center"
:width="item.width"
:label="item.label"
v-if="item.show"
>
></el-table-column>

编辑器提示:

vue/no-use-v-if-with-v-for] The 'columns' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.eslint-plugin-vue

修改后:

<template v-for="(item, index) in columns">
<el-table-column
:prop="item.prop"
:key="index"
align="center"
:width="item.width || '' "
:label="item.label"
v-if="item.show"
>
</el-table-column>
</template>
  1. 即使用
    template
    标签包裹即可,
    v-for
    写在
    template
    上,
    v-if
    绑定在需要循环的元素之上即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: