Search code examples
javascripttypescriptvue.jsvuejs2vue-component

Vuejs typescript this.$refs.<refField>.value does not exist


While rewriting my VueJs project in typescript, I came across a TypeScript error.

This is a part of the component that has a custom v-model.

An input field in the html has a ref called 'plate' and I want to access the value of that. The @input on that field calls the update method written below.

Typescript is complaining that value does not exist on plate.

@Prop() value: any;

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

template:

<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>

Solution

  • You can do this:

    class YourComponent extends Vue {
      $refs!: {
        checkboxElement: HTMLFormElement
      }
    
      someMethod () {
        this.$refs.checkboxElement.checked
      }
    }
    

    From this issue: https://github.com/vuejs/vue-class-component/issues/94