【问题标题】:vue 3 composition api props function Cannot invoke an object which is possibly 'undefined'vue 3组合api道具功能无法调用可能是“未定义”的对象
【发布时间】:2021-07-23 13:44:16
【问题描述】:

我有一个名为 sortSearch 的道具,它是来自父调用者的函数或回调。

type SortSearchInterface = (
 currentSort: string,
 currentSortDir: string
) => void;

export default defineComponent({
  props: {
    //sortSearch: {
    //type: Function as PropType<(currentSort: string, currentSortDir: string)=> void>
    // type: Function as PropType<SortSearchInterface>|undefined
    // }
    sortSearch: Function as PropType<SortSearchInterface>,
  },

attempting to call this function 出现以下错误Cannot invoke an object which is possibly 'undefined'

     // Cannot invoke an object which is possibly 'undefined'.Vetur(2722)
      //props.sortSearch(s, state.currentSortDir);

通过我的简短谷歌搜索,发现这方面的示例/文档非常有限,感谢任何帮助。

【问题讨论】:

    标签: typescript vuejs3 vue-composition-api


    【解决方案1】:

    您需要根据需要标记此道具:

      props: {
        sortSearch: {
          type: Function as PropType<SortSearchInterface>,
          required: true
        },
      },
    

    【讨论】:

    • 只是为了确认这有效,谢谢,有趣的问题和修复,不太确定如何将某些内容设置为 required = true 转换为可能未定义。它开始变得有问题的意义,因为你必须质疑它是否有意义
    • 这很有意义。没有 required: true 的 Vue 道具是可选的 - 即。该值可以是undefined...
    猜你喜欢
    • 2018-12-10
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多