【问题标题】:Skip object items if the value is null如果值为 null,则跳过对象项
【发布时间】:2016-03-24 16:48:02
【问题描述】:

我在 vue js 中有一个嵌套的 for ... in 循环。如果元素的值为null,我想要跳过元素。这是html代码:

<ul>
    <li v-for="item in items" track-by="id">
        <ol>
            <li v-for="child in item.children" track-by="id"></li>
        </ol>
    </li>
</ul>

null 元素可能同时出现在 itemitem.children 对象中。

例如:

var data = {
   1: {
      id: 1,
      title: "This should be rendered",
      children: {
          100: {
              id: 100,
              subtitle: "I am a child"
          },
          101: null
      }
   },
   2: null,
   3: {
       id: 3,
       title: "Should should be rendered as well",
       children: {}
   }
};

不应渲染此数据data[1].children[101],如果稍后data[1].children[100] 变为空,则应从列表中将其省略。

附:我知道这可能不是表示数据的最佳方式,但我对此不承担任何责任:)

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    编辑:实际上,一个简单的 v-if 可能会起作用:

    <li v-for="item in items" v-if="item !== null" track-by="id">
    

    试一试。如果没有,请执行以下操作:

    您可以为此添加一个过滤器(在 App 实例之前的 main.js 中):

    Vue.filter('removeNullProps',function(object) {
      // sorry for using lodash and ES2015 arrow functions :-P
      return _.reject(object, (value) => value === null)
    })
    

    然后在模板中:

    <li v-for="item in items | removeNullProps" track-by="id">
        <ol>
            <li v-for="child in item.children | removeNullProps" track-by="id"></li>
        </ol>
    </li>
    

    【讨论】:

    • 需要 vue-underscore 吗?
    • 仅供参考,Vue js 风格指南建议避免在同一元素上使用 v-for 和 v-if。 vuejs.org/v2/style-guide
    • 我对这个答案有一些问题。但是自从 v-if="" 我最终得到了这个 v-if="item"。如果 item 为 null,则表达式将为 null 且不为 true,因此从 DOM 中删除。如果您的意图只是隐藏,请改用 v-show。
    • -1 在v-for 中使用v-if,这是每个人都应该避免的事情。 +1 教我 Vue 过滤器,我以前从未见过使用过!
    【解决方案2】:

    在 Vue 2 中,过滤器已在 v-fors 中弃用。

    现在您应该使用计算属性。演示如下。

    new Vue({
      el: '#app',
      data: {
        items: [
          'item 1',
          'item 2',
          null,
          'item 4',
          null,
          'item 6'
        ]
      },
      computed: {
        nonNullItems: function() {
          return this.items.filter(function(item) {
            return item !== null;
          });
        }
      }
    })
    <script src="https://unpkg.com/vue@2"></script>
    <div id="app">
      Using a computed property:
      <ul>
        <li v-for="item in nonNullItems">
          {{ item }}
        </li>
      </ul>
      <hr>
      Using v-if:
      <ul>
        <li v-for="item in items" v-if="item !== null">
          {{ item }}
        </li>
      </ul>
    </div>

    【讨论】:

    【解决方案3】:

    我建议您不要在同一元素中使用 v-if 和 v-for。我发现有效且不影响性能的是:

    <li v-for="(value, key) in row.item.filter(x => x !== null)" :key="key"{{value}}</li>
    

    您只需要在您要经过的数组上运行过滤器功能。这是 c# 中的常见用法,发现它在 JavaScript 中没有什么不同。这基本上会在迭代时跳过空值。

    希望这会有所帮助(3 年后)。

    【讨论】:

      【解决方案4】:

      只需使用v-if 即可。但首先,不要使用track-by="id",因为null 项和null 子项。你可以在这里查看演示https://jsfiddle.net/13mtm5zo/1/

      也许更好的方法是在渲染之前先处理数据。

      【讨论】:

        【解决方案5】:

        VueJs 风格指南告诉我们:

        “永远不要在与 v-for 相同的元素上使用 v-if。”

        如何根据 vue 风格指南正确处理 v-if 和 v-for :

        <ul v-if="shouldShowUsers">
          <li
            v-for="user in users"
            :key="user.id"
          >
            {{ user.name }}
          </li>
        </ul>
        

        在此处查看有关如何使用v-for 处理v-if 的更多信息:https://vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential

        【讨论】:

        • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
        • 嘿@NicoHaase 感谢您的评论,这是我在这里的第一篇文章!我用一个真实的例子编辑了我的帖子。
        • 你好,如果用户=== null,它会得到component lists rendered with v-for should have explicit keys这个怎么处理?
        • @AgnesPalit 而不是用户 ID,您可以像这样使用元素数组索引 &lt;li v-for="(user, index) in users" :key="index"&gt;...
        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 2018-08-20
        • 2017-12-28
        • 2014-12-10
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 2019-12-19
        相关资源
        最近更新 更多