Vuejs | 从 'vue' 导入 { PropType } 错误

ShC*_*hCc 1 vue.js vite

错误:“PropType”是一种类型,当“preserveValueImports”和“isolatedModules”均启用时,必须使用仅类型导入进行导入。ts(1444)

我的“JobsList.vue”组件:

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

import type Job from '../types/job'

export default defineComponent({
    props: {
        jobs: {
            required: true,
            type: Array as PropType<Job[]>
        }
    }
})
</script>


<template>
    <section>
        <ul>
            <li v-for="job in jobs" :key="job.id">{{ job.title }}</li>
        </ul>
        
    </section>
</template>
Run Code Online (Sandbox Code Playgroud)

我的 src/types/Job.ts:

interface Job {
    title: string;
    location: string;
    salary: number;
    id: string;
}

export default Job;
Run Code Online (Sandbox Code Playgroud)

依赖版本:

  "dependencies": {
    "vue": "^3.2.41"
  },
  "devDependencies": {
    "@types/node": "^16.11.68",
    "@vitejs/plugin-vue": "^3.1.2",
    "@vue/tsconfig": "^0.1.3",
    "npm-run-all": "^4.1.5",
    "typescript": "~4.7.4",
    "vite": "^3.1.8",
    "vue-tsc": "^1.0.8"
  }
Run Code Online (Sandbox Code Playgroud)

我只是尝试更改 tsconfig.config.json 中的“isolatedModules”和“preserveValueImports”布尔值

小智 6

您需要更改这一行:

import { defineComponent, PropType} from 'vue'
Run Code Online (Sandbox Code Playgroud)

对于这些行:

import { defineComponent } from 'vue'
import type { PropType } from 'vue'
Run Code Online (Sandbox Code Playgroud)