Home  >  Article  >  Web Front-end  >  How to solve the pitfalls encountered by VUE3+TS in obtaining component types

How to solve the pitfalls encountered by VUE3+TS in obtaining component types

WBOY
WBOYforward
2023-05-13 14:46:061043browse

    VUE3 TS method of obtaining component type pitfalls

    Method of obtaining component type

    const AccountRef = ref<InstanceType<typeof LoginAccount>>()

    pitfalls encountered

    typeof LoginAccount keeps reporting a red line prompt error

    LoginAction: () => vo...&#39; provides no match for the signature &#39;new (...args: any): any&#39;.

    Cause of the problem

    I use webstorm coding, and create a new vue file through webstorm's own right-click, and the created file does not have defineComponent, so Resulting in the above red line that always reports

    <script lang="ts">
    export default {
    
    }
    </script>

    Solution

    Reference defineComponent

    <script lang="ts">
    import { defineComponent } from &#39;vue&#39;
    export default defineComponent({//注意这里的这个小括号不能漏
    
    })
    </script>

    VUE3 TS obtains the component ref instance

    When using vue3 and ts, in order to obtain For component ref instances, you need to specify the type in the generic type of the ref function.

    How to get the type of component?

    TypeScript support in the official documentation of vue has told us a method to obtain the component type. InstanceTypef9b6eb30120e4f46d5ca46914342d2de

    The usage method is as follows:

    const $userForm = ref<InstanceType<typeof userForm>>();

    The above is the detailed content of How to solve the pitfalls encountered by VUE3+TS in obtaining component types. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete