【发布时间】:2017-02-27 11:21:16
【问题描述】:
我有一个输入没有任何 Vue 指令:
<input type="text" id="name" />
现在我正在构建一个自定义 Vue 组件,它接受一个名为 input 的参数,该参数应该是该输入的 DOM 选择器:
<component input="#name"></component>
现在我想根据输入的值切换组件模板中的一个类,类似于表单输入绑定。但是,由于我们在组件内部,我猜无法使用模型绑定。所以我尝试创建一个计算属性:
<template>
<div v-bind:class="[inputValue ? 'active' : '']"></div>
</template>
<script>
module.exports = {
props: [
'input'
],
computed: {
inputValue: function () {
return $(this.input).val();
}
}
}
</script>
很遗憾,这不起作用。有人知道在此组件中查看“外部”输入值的解决方法吗?
(注意:为简洁起见,省略了组件的主要功能)
【问题讨论】: