如何将 v-if 与 Vue.js 中的值进行比较

How to compare v-if with a value in Vue.js

我有以下数据,我想检查用户是否具有管理员角色。

{
    "users": [
      {
        "id": 3,
        "first_name": "Joe",
        "last_name": "Blogs",
        "roles": [
          {
            "id": 1,
            "name": "Admin",
            "created_at": "2015-12-25 15:58:28",
            "updated_at": "2015-12-25 15:58:28",
            "pivot": {
              "user_id": 3,
              "role_id": 1,
              "created_at": "2015-12-25 16:03:09",
              "updated_at": "2015-12-25 16:03:09"
            }
          },
          {
            "id": 2,
            "name": "Member",
            "created_at": "2015-12-25 15:58:28",
            "updated_at": "2015-12-25 15:58:28",
            "pivot": {
              "user_id": 3,
              "role_id": 2,
              "created_at": "2015-12-25 16:03:09",
              "updated_at": "2015-12-25 16:03:09"
            }
          }
        ],
      }
    ]
  } 

在我的 html 中,我有以下内容,但是如何使用 v-if 指令检查用户是否具有管理员角色?另一个问题是如果条件不正确,下面的 v-if 指令也会生成不必要的 html 标记。

    <tr>
        <th>User</th>
        <th>Admin</th>
        <th></th>
    </tr>
    <tr v-for="user in users">
          <td>
               @{{user.first_name}} @{{user.last_name}}
          </td>
          <td>
              <span v-for="role in user.roles">
                   <span v-if="role.name" == 'Admin'>Yes</span>
                   <span v-else>-</span>
              </span>
          </td>
    </tr>

你的语法有问题html,应该是:

<span v-for="role in user.roles">
    <span v-if="role.name == 'Admin'">Yes</span>
    <span v-else>-</span>
</span>

不过,对于用户拥有的每个角色,您仍然会有额外的跨度。

使用一种方法检查用户是否具有管理员角色更简单,这样您就不会拥有额外的 html:

<div id="container">
  <table>
    <tr>
      <th>User</th>
      <th>Admin</th>
      <th></th>
    </tr>
    <tr v-for="user in users">
      <td>
        {{user.first_name}} {{user.last_name}}
      </td>
      <td>
        {{isAdmin(user)}}
      </td>
    </tr>
  </table>
</div>

和 javascript:

new Vue({
  el: '#container',
  data: {
    "users": [{
      "id": 3,
      "first_name": "Joe",
      "last_name": "Blogs",
      "roles": [{
        "id": 1,
        "name": "Admin",
        "created_at": "2015-12-25 15:58:28",
        "updated_at": "2015-12-25 15:58:28",
        "pivot": {
          "user_id": 3,
          "role_id": 1,
          "created_at": "2015-12-25 16:03:09",
          "updated_at": "2015-12-25 16:03:09"
        }
      }, {
        "id": 2,
        "name": "Member",
        "created_at": "2015-12-25 15:58:28",
        "updated_at": "2015-12-25 15:58:28",
        "pivot": {
          "user_id": 3,
          "role_id": 2,
          "created_at": "2015-12-25 16:03:09",
          "updated_at": "2015-12-25 16:03:09"
        }
      }],
    }]
  },
  methods: {
    isAdmin: function(user) {
      var i;
      for (i = 0; i < user.roles.length; i++) {
        if (user.roles[i].name === "Admin") {
          return "Yes";
        }
      }

      return "-";
    }
  }
});

这里的工作示例:https://jsfiddle.net/gmsa/t56oo4tw/