如您所知,从Vue 3开始,组件可以用TypeScript编写:
/// 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实例?
/// another ts file.
let modal:???; // what the `???` should be?
modal.$props.foo // infer `$props.foo` correctly
<script>
标签的组件(例如仅有<template>
的组件),会抛出类型错误。 - Letik