Vuejs打字稿this。$ refs。 。价值不存在

问题描述:

在打字稿中重写我的VueJs项目时,我遇到了TypeScript错误。

这是具有自定义v模型的组件的一部分。

html中的输入字段有一个名为'plate'的ref,我想访问它的值。该字段上的@input调用下面所写的更新方法。

Typescript抱怨板上没有价值。

@Prop() value: any; 

update() { 
    this.$emit('input', 
     plate: this.$refs.plate.value 
    }); 
} 

模板:

<template> 
<div> 
    <div class="form-group"> 
     <label for="inputPlate" class="col-sm-2 control-label">Plate</label> 

     <div class="col-sm-10"> 
      <input type="text" class="form-control" id="inputPlate" ref="plate" :value="value.plate" @input="update"> 
     </div> 
    </div> 

</div> 
</template> 
+0

你的模板是什么样的? – thanksd

+0

@thanksd我将它添加到问题中 – Rick

我找到了一种方法,使其工作,但在我看来是丑陋的。

随意给其他/更好的建议。

update() { 
    this.$emit('input', { 
     plate: (<any>this.$refs.plate).value, 
    }); 
}