javascript - V-bind等级多种选择

标签 javascript class vue.js

我试图根据三个可能的输入变量之一为元素提供两个类之一。 我的 vue.js 代码

<input type='text' class='inputwordtext' v-bind:class="[{(wordupload.firstchoice.selected == 'Zinnenlijst') : wordlongwidth}, {(wordupload.firstchoice.selected != 'Zinnenlijst') : wordshortwidth}]">

如果 wordupload.firstchoice.selected == Zinnenlijst 它应该获取类 wordlongwidth,否则它应该获取类 wordshortwidth,这是怎么做到的?

最佳答案

您可以使用十元运算符通过单个内联表达式来完成此操作:

<input 
  type='text' 
  class='inputwordtext'
  :class="wordupload.firstchoice.selected === 'Zinnenlijst' ? 'wordlongwidth' : 'wordshortwidth'"
>
<小时/>

但是,将其设为计算属性会更具可读性:

computed: {
  inputClass() {
    let selected = this.wordupload.firstchoice.selected;
    return (selected === 'Zinnenlijst') ? 'wordlongwidth' : 'wordshortwidth';
  }
}

然后在模板中引用该计算属性:

<input type='text' class='inputwordtext' :class="inputClass">

关于javascript - V-bind等级多种选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46651769/

相关文章:

javascript - 如何制作 Google 混搭,允许多个用户在 map 上精确定位多个位置?

javascript - Gulp autoprefixer、browserify 和浏览器支持

php类调用外部类

javascript - 我想将对象插入 Vue.js 中的对象数组中

javascript - 当数据数组插入新元素时如何更新子组件?

javascript - Vue.js 应用程序从新页面的下拉菜单中打开 url 链接

javascript - 在 Vue.js 中将外部图像拖过(dragenter)时在放置区域(文件输入)添加类

javascript - Node.js - 无法删除使用 createWriteStream 创建并写入的文件

html - 使用 CSS 和 HTML 内联显示

c++ - 我怎样才能写出干净利落的c++构造函数?