首页  >  问答  >  正文

如何在vue.js + typescript中声明对象数组的数据

我在 vue.js 3 + typescript 中有这段代码,我只想在数据中声明一个对象数组,但弹出几个错误

<template>
  <ul >
    <li v-if="!items.length">...loading</li>
    <li v-for="item in items" :key="item.id">
        <!--{{item}}-->
        <!--donde va {{item}} pongo un slot tag con un name y binding el item que voy a utilizar en el parent-->
        <slot name="item" v-bind="item"></slot>

    </li>
  </ul>
</template>

<script lang="ts">
import { defineComponent } from 'vue'


export default defineComponent({
    name: 'child-slot-component',
    data() {
    return {
        items: []
    }
  },
    mounted() {
        setTimeout(() => {
      this.items = [
        { id: 1, body: 'Scoped Slots Guide', username: 'Evan You', likes: 20 },
        { id: 2, body: 'Vue Tutorial', username: 'Natalia Tepluhina', likes: 10 }
      ]
    }, 1000)
    },
})
</script>

<style  scoped>
    ul{
        list-style-type: none;
    }

</style>

id、正文、用户名和点赞类型“number”或“string”不可分配给“never”类型。ts(2322) 因为在数据中,当声明具有空数组的项目时,它说 property:never 并且我想声明如下内容: {id:number, body:string,username:string, likes:number}[]

如果不正确,使用打字稿处理此声明的正确方法是什么,或者我可能需要配置 tsconfig.json 或其他内容

提前致谢 哦,该应用程序运行良好!!

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "useDefineForClassFields": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

这是我的 vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
})

我的巴贝尔配置

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ]
}

我的 shims-vue.d.ts

/* eslint-disable */
declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}

和我的 .eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true
  },
  'extends': [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/typescript/recommended'
  ],
  parserOptions: {
    ecmaVersion: 2020
  },
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  }
}

只是带vue create app-name的默认配置

P粉818317410P粉818317410373 天前731

全部回复(1)我来回复

  • P粉775723722

    P粉7757237222023-11-03 09:05:25

    提前使用类型定义项目数组应该可以修复 TypeScript 错误。像这样的东西应该有效:

    items: [] as { id: number, body: string, username: string, likes: number }[]

    回复
    0
  • 取消回复