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

前端 未结 7 1449
野的像风
野的像风 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:11

    Avoid using bracket < > to typecast because it will conflict with JSX.

    Try this instead

    update() {
        const plateElement = this.$refs.plate as HTMLInputElement
        this.$emit('input', { plate: plateElement.value });
    }
    

    as a note that I always keep remembering

    Typescript is just Javascript with strong typing capability to ensure type safety. So (usually) it doesn't predict the type of X (var, param, etc) neither automatically typecasted any operation.

    Also, another purpose of the typescript is to make JS code became clearer/readable, so always define the type whenever is possible.

    0 讨论(0)
提交回复
热议问题