首页  >  问答  >  正文

当我在 vue 3 脚本设置中将数组作为 prop 传递时,如何在 DefineProps 中初始化/为数组提供默认值

我已经传递了一个数组作为道具项,我在接口 Props 中给了它一个类型,当我尝试给它一个默认值时,我收到错误第四行 TS2322:类型'never[]'不可分配给类型'(道具:只读<Props>)= >字符串[]'。类型“never[]”不提供与签名“(props: Readonly<Props>): string[]”的匹配项。 我不确定我在这里做错了什么,因为这似乎适用于其他变量

<script setup lang="ts">
import {ref} from "vue";

interface Props {
  items?: Array<string>
}

const props = withDefaults(defineProps<Props>(), {
  items: []
});
let selectedItem = ref(props.items[0])

P粉155551728P粉155551728207 天前421

全部回复(1)我来回复

  • P粉436688931

    P粉4366889312024-03-26 17:16:21

    文档

    这适用于选项 API 和组合 API!

    const props = withDefaults(defineProps(), {
      items: () => []
    });
    

    复制

    回复
    0
  • 取消回复