Vuejs typescript this.$refs..value does not exist

前端 未结 7 1451
野的像风
野的像风 2020-11-30 13:33

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 i

7条回答
  •  一整个雨季
    2020-11-30 14:07

    Edit - 2020-04:

    The vue-property-decorator library provides @Ref which I recommend instead of my original answer.

    import { Vue, Component, Ref } from 'vue-property-decorator'
    
    import AnotherComponent from '@/path/to/another-component.vue'
    
    @Component
    export default class YourComponent extends Vue {
      @Ref() readonly anotherComponent!: AnotherComponent
      @Ref('aButton') readonly button!: HTMLButtonElement
    }
    

    Original Answer

    None of the above answers worked for what I was trying to do. Adding the following $refs property wound up fixing it and seemed to restore the expected properties. I found the solution linked on this github post.

    class YourComponent extends Vue {
      $refs!: {
        vue: Vue,
        element: HTMLInputElement,
        vues: Vue[],
        elements: HTMLInputElement[]
      }
    
      someMethod () {
        this.$refs..
      }
    }
    

提交回复
热议问题