Vue with TypeScript:如何以类型安全的方式访问组件属性

huangapple go评论62阅读模式
英文:

Vue with TypeScript: How to access component properties in type safe way

问题

Sure, here are the translated parts of your code:

我对Vue的setup API还不太了解,有一件事仍然困扰着我:我需要从父组件访问子组件的属性。它可以正常工作,但我无法正确定义TypeScript的类型。我总是收到TS错误,说属性不存在。从TypeScript的角度看,似乎是正确的,因为在我定义的对象中确实没有该属性。但一定有办法让TS理解

<template>
  <child ref="child" />
</template>

<script lang="ts">
import Child from 'child.vue';

export default {
  name: 'parent',
  mounted() {
    const child: Child = this.$refs.child;
    console.log(child.myprop); // TS2339: Property 'myprop' does not exist on type...
  }
}
</script>
<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'child',
  setup(props, context) {
    const myprop = 1;
    context.expose({ myprop });
    return { myprop };
  }
})
</script>
英文:

I am new to Vue's setup API and one thing still eludes me: I need access to properties of a child component from a parent component. It works fine, but I cant get the typescript definition right. I always get the TS error, that the propery does not exist. And from TypeScript perspective it seems to be correct, as there is no property in the object I defined. But there must be a way to make TS understand.

&lt;template&gt;
  &lt;child ref=&quot;child&quot; /&gt;
&lt;/template&gt;

&lt;script lang=&quot;ts&quot;&gt;
import Child from &#39;child.vue&#39;

export default {
  name: &#39;parent&#39;,
  mounted() {
    const child: Child = this.$refs.child;
    console.log(child.myprop); // TS2339: Property &#39;myprop&#39; does not exist on type...
  }
}
&lt;/script&gt;
&lt;script lang=&quot;ts&quot;&gt;
import {defineComponent} from &#39;vue&#39;;

export default defineComponent({
  name: &#39;child&#39;,
  setup(props, context) {
    const myprop = 1;
    context.expose({myprop});
    return {myprop};
  }
})
&lt;/script

答案1

得分: 1

你在这里混合使用了组合式 API 和选项式 API。

如果将父组件重写为使用组合式 API,与类型一起工作会更容易:

import Child from 'child.vue'

export default {
  name: 'parent',
  setup() {
    const childRef = ref<InstanceType<typeof Child>>();

    onMounted(() => {
      console.log(childRef.value?.myProp);
    })

    return { childRef };
  }
}
英文:

You are mixing Composition API and Options API here.

if you rewrite the parent component to use Composition API, working with types would be easier:

import Child from &#39;child.vue&#39;
    
export default {
  name: &#39;parent&#39;,
  setup() {
    const childRef = ref&lt;InstanceType&lt;typeof Child&gt;&gt;();

    onMounted(() =&gt; {
      console.log(childRef.value?.myProp);
    })

    return { childRef };
  }
}

答案2

得分: 1

尝试这个:

const child: InstanceType<typeof Child> = this.$refs.child;

或者这个:

const child = this.$refs.child as Child;
英文:

Try this:

const child: InstanceType&lt;typeof Child&gt; = this.$refs.child;

or this:

const child = this.$refs.child as Child;

huangapple
  • 本文由 发表于 2023年5月10日 18:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定