javascript - Vue.js:类切换的条件

标签 javascript vue.js conditional-statements

我已经为侧边栏中的项目设置了一个 active-class,但是当该项目处于事件状态时,如何更改其标签的类?我需要带有 class="u-label g-font-size-11 g-bg-primary g-rounded-20 g-px-7 g-ml-3"的 span 拥有 class="u-label g-font-size-11 g-bg-primary g-rounded-20 g-px-7 g-ml-3 g-color-primary g- bg-white” 当我点击“通知”(使项目处于事件状态)时。

<router-link to='/notifications'
                 class="list-group-item justify-content-between g-brd-none list-group-item-action"
                 active-class="active">
      <i class="icon-bell g-pos-rel g-top-1 g-mr-8"></i>
      Оповещения
      <span v-if="getNotificationsUnviewed > 0"
            class="u-label g-font-size-11 g-bg-primary g-rounded-20 g-px-7 g-ml-3">
             {{ getNotificationsUnviewed }}
      </span>
    </router-link>

最佳答案

您需要使用 v-bind 向类传递具有 true/false 值的对象,如下所示:

<span v-if="getNotificationsUnviewed > 0"
    v-bind:class="{'u-label g-font-size-11 g-bg-primary g-rounded-20 g-px-7 g-ml-3' : true, 'g-color-primary g-bg-white' : isActive}">
         {{ getNotificationsUnviewed }}
  </span>

让我们看看:class里面有什么:

{
  "u-label g-font-size-11 g-bg-primary g-rounded-20 g-px-7 g-ml-3": true,
  "g-color-primary g-bg-white": isActive
}

第一个类设置将始终存在,因为值为 true,第二个类仅在 isActivetrue 时设置。

当然,您的实例/组件必须具有 isActive 属性/数据才能使其正常工作。

See the docs了解更多信息和替代方法。

关于javascript - Vue.js:类切换的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48582515/

相关文章:

vue.js - 当我添加 vue-i18n (karma + mocha + phantomJs) 时,Vue 项目测试失败

html - 单击按钮并转到 Vue.js 中的页面部分

python - 基于其他行的过去值到当前值的新行

R:根据现有数据帧上的多个条件添加数据列和行

javascript - 如何在iOS自动化脚本中搜索字符串中的子字符串? indexOf() 和 search() 方法不起作用

javascript - Appcelerator Titanium 中用于 OOP 的 CommonJS

javascript - 如何使用 dojo 提交使用 spring 表单标签创建的表单

javascript - 如何添加安全性以便模式窗口不会从浏览器的 html 代码中删除?

javascript - 使用vue,我希望一个组件位于页面的最顶部,但仅限于单个 View

c++ - 哪个更快 : Assigning same value to a variable, 或找出它是否已经设置?