首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在vue 3中的` `defineComponent()`中键入vue实例?

如何在vue 3中的` `defineComponent()`中键入vue实例?
EN

Stack Overflow用户
提问于 2020-09-21 11:05:07
回答 1查看 3.8K关注 0票数 5

正如您所知道的,从Vue 3开始,组件可以用TypeScript编写:

代码语言:javascript
复制
/// modal.vue

<template>
  <div class="modal"></div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Modal",
  props: {
    foo: String,
    bar: String
  },
  mounted() {
    this.$props.foo // how to type `this` out of this context?
  }
});
</script>

我的问题是,如何从defineComponent函数中键入vue实例?

代码语言:javascript
复制
/// another ts file.
let modal:???; // what the `???` should be?

modal.$props.foo // infer `$props.foo` correctly
EN

回答 1

Stack Overflow用户

发布于 2020-09-21 12:09:13

我打算给出的“简单”答案是使用ReturnType<typeof defineComponent>,但是它不包含任何类型信息。当我开始研究如何在泛型方法中使用ReturnType时,我被stackoverflow rabbit hole where these seemed like something to explore迷住了

但是,在查看之后,vue有一个导出类型ComponentPublicInstance,可以相当容易地使用。ComponentPublicInstance也有一些不同的泛型参数。

代码语言:javascript
复制
import { ComponentPublicInstance } from 'vue';

let instance: ComponentPublicInstance<{ prop: string }, { value: string }>;
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63985658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档