Home > Web Front-end > Vue.js > 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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-05-13 14:46:06
forward
1144 people have browsed it

    VUE3 TS method of obtaining component type pitfalls

    Method of obtaining component type

    const AccountRef = ref<InstanceType<typeof LoginAccount>>()
    Copy after login

    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;.
    Copy after login

    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>
    Copy after login

    Solution

    Reference defineComponent

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

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

    The usage method is as follows:

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

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template