javascript - Vue 类与字符串绑定(bind)?

标签 javascript css vue.js

有没有办法让 Vue 类绑定(bind)与字符串一起使用? 例如:

<div :class={open: target == 'myString'}></div>
var app = new Vue({
  target: null
})

不加引号就可以了

<div :class={open: target == myString}></div>

但我收到以下警告:

[Vue warn]: Property or method "myString" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

最佳答案

尝试以下操作

var app = new Vue({
  el: '#app', // the id of the div that you will apply this object on it
  data : {
    target: '',
    myString: '',
  }
})

关于javascript - Vue 类与字符串绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50252604/

相关文章:

javascript - 获取响应未设置浏览器 cookie

javascript - jquery 嵌套选择不起作用

javascript - 如何将 span 元素添加到 img,然后用链接包装所有内容?

css - Chrome 中 Sass 样式表的 MIME 类型为文本/纯文本

javascript - 设置 innerHTML 之间的转换

html - 两段叠加一张图片

vue.js - 为什么 '@drop'事件在vue中对我不起作用?

javascript - GIF 不适用于 Safari 中的表单提交,但适用于 Chrome

vue.js - Nuxt 无法加载 payload _payload.js TypeError : Failed to resolve module specifier '_payload.js'

javascript - 如何使用 html 中的 Vue.js 从 Web 服务获取数据?