简体   繁体   English

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

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

My code looks like this,我的代码看起来像这样,

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

Am I missing something, because I am getting error on this line:我错过了什么吗,因为我在这一行遇到错误:

错误截图

I also tried solution from this page, but nothing works.我也尝试了此页面的解决方案,但没有任何效果。

Vue 2 - How to set default type of array in props Vue 2 - 如何在道具中设置默认数组类型

This is not an error, but a warning that the anonymous function should indicate a return type.这不是错误,而是匿名 function 应指示返回类型的警告。 Your IDE shows the name of the check, and searching for it leads to a page with good examples .您的 IDE 显示了支票的名称,搜索它会转到一个包含很好示例的页面。

The warning should be fixed like this:警告应该像这样修复:

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

As error said, you are missing function return type.正如错误所说,您缺少 function 返回类型。 You defined prop type, but not function return type.您定义了道具类型,但没有定义 function 返回类型。

It should look like this (in place of any you should also specify type):它应该看起来像这样(代替任何你还应该指定类型):

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 . 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 If you are using the recommended linting settings (and your IDE is working correctly) the problem should be solved如果您使用推荐的 linting 设置(并且您的 IDE 工作正常),问题应该得到解决

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

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