Vue 怎么向子组 传递一个 ts type类弄
时间: 2023-05-23 21:03:23 浏览: 52
您可以通过使用Vue中的Props属性向子组件传递ts type类。在父组件中声明Prop属性,并将类型指定为所需的ts type类,然后将其传递给子组件作为属性。在子组件中可以通过使用@Prop装饰器来接收该属性,并指定类型为所需的ts type类。例如,
<template>
<child-component :myProp="myData"></child-component>
</template>
<script lang="ts">
import { Component, Vue, Prop } from 'vue';
@Component({
components: { ChildComponent }
})
export default class ParentComponent extends Vue {
@Prop({ type: MyTsType }) myProp!: MyTsType;
myData = {...};
}
</script>
<template>
<div>{{ myProp.myValue }}</div>
</template>
<script lang="ts">
import { Component, Vue, Prop } from 'vue';
import { MyTsType } from './MyTsType';
@Component
export default class ChildComponent extends Vue {
@Prop({ type: MyTsType }) myProp!: MyTsType;
}
</script>