繁体   English   中英

在带有 Typescript 的 Vue 3 中的 Function 上缺少返回类型

[英]Missing return Type on Function in Vue 3 with Typescript

我的代码看起来像这样,

    <template>
      <div>        
        <ul v-if="list.length !== 0">
          {{
            list
          }}
        </ul>
      </div>
    </template>
    <script>
    export default {
      props: {
        
        list: {
          type: Array,
          default: () => {
            return []
          },
        },
        
      },
    }
    </script>

我错过了什么吗,因为我在这一行遇到错误:

错误截图

我也尝试了此页面的解决方案,但没有任何效果。

Vue 2 - 如何在道具中设置默认数组类型

这不是错误,而是匿名 function 应指示返回类型的警告。 您的 IDE 显示了支票的名称,搜索它会转到一个包含很好示例的页面。

警告应该像这样修复:

list: {
  type: Array,
  default: ():Array => {
    return []
  },
},

正如错误所说,您缺少 function 返回类型。 您定义了道具类型,但没有定义 function 返回类型。

它应该看起来像这样(代替任何你还应该指定类型):

export default {
  props: {
    list: {
      type: Array,
      default: () : Array<any> => {
        return []
      },
    },
  },
}

As a general hint I would suggest that if you are using Vue 3 + Typescript you should take advantage of the defineComponent function to wrap your component options object as documented here: https://v3.vuejs.org/guide/typescript-support. html#defining-vue-components 如果您使用推荐的 linting 设置(并且您的 IDE 工作正常),问题应该得到解决

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM