vue将道具放入未使用的打字稿设置中的组件
我试图将道具带入我的组件中。 找到了此文档: 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
defineProps
只能在&lt; script设置中使用,
(ts
或js
)。在您发布的示例中,您正在尝试在
&lt; script&gt;
tag的root中使用它(没有设置
属性!),其中它不会工作。要澄清:
&lt; script设置lang =“ ts”&gt;
语法:&lt; script lang =“ ts”&gt;
syntax:See them working here 。
defineProps
can only be used inside<script setup>
(ts
orjs
).In the example you posted, you're trying to use it inside a
<script>
tag's root (does not have thesetup
attribute!), where it won't work.To clarify:
<script setup lang="ts">
syntax:<script lang="ts">
syntax:See them working here.
在我看来,您已经混合了两种方式。
您是否尝试过下面的工作?
参见链接。
It seems to me that you have mixed the two ways.
Have you tried to do as below?
See the link.