vue将道具放入未使用的打字稿设置中的组件

发布于 2025-01-30 12:28:20 字数 1922 浏览 0 评论 0原文

我试图将道具带入我的组件中。 找到了此文档: https://vuejs.s.s.org/guide/guide/guide/guide/guide/typescript-croptirect-croptript--croptirect--croptript--compoction--croptription-position-position-position-position-position-position-position--cromit- api.html#键入 - 典型 - ,它在无处不在的地方引用。 显然,我应该将我的类型放入DefineProps功能中。

<script setup lang="ts">
const props = defineProps({
  foo: { type: String, required: true },
  bar: Number
})

props.foo // string
props.bar // number | undefined - they are not usable in the template if I just define the const...
</script>

因此,我试图将它们放在我的组件上。但是要这样做,我需要在设置中进行STH。

import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    message: String
  },
  setup(props) {
    props.message // <-- type: string
  }
})
<template>
    <CNavItem :href="props.href">
    <CIcon customClassName="nav-icon" :icon="icon" />{{label}}</CNavItem>
</template>

<script lang="ts">
import { defineComponent, defineProps } from "vue";
import { CNavItem } from '@coreui/vue';
import { CIcon } from '@coreui/icons-vue';

interface Props {
    label: string;
    href: string;
    icon: any[];
}

const props = defineProps<Props>();
export default defineComponent({
    components: {
        CNavItem,
        CIcon
    },
    setup(props) {
        // I need to do sth with props here I guess, but I do not know how to get it to my template.
        // If I don't the compiler will complain...
    }
});
</script>

我想我在这里有一个Missig链接。那么,如何将我的const props的内容纳入我的模板中?

I am trying to bring my props into my component.
Found this documentation:
https://vuejs.org/guide/typescript/composition-api.html#typing-component-props which is referenced close to everywhere.
Obviously I should put my type into the defineProps function.

<script setup lang="ts">
const props = defineProps({
  foo: { type: String, required: true },
  bar: Number
})

props.foo // string
props.bar // number | undefined - they are not usable in the template if I just define the const...
</script>

So I tried to put them to my component. But to do so I need to do sth within the setup.

import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    message: String
  },
  setup(props) {
    props.message // <-- type: string
  }
})
<template>
    <CNavItem :href="props.href">
    <CIcon customClassName="nav-icon" :icon="icon" />{{label}}</CNavItem>
</template>

<script lang="ts">
import { defineComponent, defineProps } from "vue";
import { CNavItem } from '@coreui/vue';
import { CIcon } from '@coreui/icons-vue';

interface Props {
    label: string;
    href: string;
    icon: any[];
}

const props = defineProps<Props>();
export default defineComponent({
    components: {
        CNavItem,
        CIcon
    },
    setup(props) {
        // I need to do sth with props here I guess, but I do not know how to get it to my template.
        // If I don't the compiler will complain...
    }
});
</script>

I think I have a missig link somewhere here. So how would I get my content of const props into my template?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装迷糊 2025-02-06 12:28:20

defineProps只能在&lt; script设置中使用,tsjs)。
在您发布的示例中,您正在尝试在&lt; script&gt; tag的root中使用它(没有设置属性!),其中它不会工作。


要澄清:

  • &lt; script设置lang =“ ts”&gt;语法:
<script setup lang="ts">
defineProps({
  foo: Number
})
</script>
<template>
  <div>{{ foo }}</div>
</template>
  • &lt; script lang =“ ts”&gt; syntax:
<script lang="ts">
export default {
  props: {
    foo: Number
  }
}
</script>
<template>
  <div>{{ foo }}</div>
</template>

See them working here

defineProps can only be used inside <script setup> (ts or js).
In the example you posted, you're trying to use it inside a <script> tag's root (does not have the setup attribute!), where it won't work.


To clarify:

  • <script setup lang="ts"> syntax:
<script setup lang="ts">
defineProps({
  foo: Number
})
</script>
<template>
  <div>{{ foo }}</div>
</template>
  • <script lang="ts"> syntax:
<script lang="ts">
export default {
  props: {
    foo: Number
  }
}
</script>
<template>
  <div>{{ foo }}</div>
</template>

See them working here.

童话 2025-02-06 12:28:20

在我看来,您已经混合了两种方式。

您是否尝试过下面的工作?

import { defineComponent } from 'vue'

export default defineComponent({
  // type inference enabled
  props: {
    label: String,
    href: String,
    icon: any[]
  },
  setup(props) {
    props.label // ...
    // ...
  }
})

参见链接

It seems to me that you have mixed the two ways.

Have you tried to do as below?

import { defineComponent } from 'vue'

export default defineComponent({
  // type inference enabled
  props: {
    label: String,
    href: String,
    icon: any[]
  },
  setup(props) {
    props.label // ...
    // ...
  }
})

See the link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文