英文:
Nuxt 3.5.1 get error on build " ERROR Cannot read properties of undefined (reading 'sys') (x4) "
问题
我使用Nuxt 3.5.1制作了一个应用程序,以下是script标签的内容:
<script lang="ts" setup>
import { IProduct } from './types';
const p = defineProps<IProduct>();
</script>
types.ts文件的内容如下:
export type IProduct = {
name?: string;
id?: number;
price?: number;
thumbnail?: string;
title: string;
}
export interface IProductSecond {
n: string;
id: number;
}
但是当我运行以下命令时:
npm run dev
我收到以下错误消息:
如果我将以下代码:
const p = defineProps<IProduct>();
替换为:
const p = defineProps<{ name?: string;
id?: number;
price?: number;
thumbnail?: string;
title: string;}>();
那么它可以正常工作。
英文:
I have an application made using Nuxt 3.5.1 this is the script tag :
<script lang="ts" setup>
import { IProduct } from './types';
const p = defineProps<IProduct>();
</script>
types.ts :
export type IProduct = {
name?: string;
id?: number;
price?: number;
thumbnail?: string;
title: string;
}
export interface IProductSecond {
n: string;
id: number;
}
but when I run
>npm run dev
If I replace :
const p = defineProps<IProduct>();
with
const p = defineProps<{ name?: string;
id?: number;
price?: number;
thumbnail?: string;
title: string;}>();
It works fine
答案1
得分: 3
不久前,Vue 3.3发布,目前Nuxt不支持Vue 3.3的“Imported Types”。有一种解决方法:
- 如果
package.json
中没有typescript
,请添加npm i -D typescript
(required) - 在你的
nuxt.config.ts
中添加以下代码:
import { existsSync, readFileSync } from "node:fs";
export default defineNuxtConfig({
vite: {
vue: {
script: {
fs: {
fileExists(file: string) {
return existsSync(file);
},
readFile(file: string) {
return readFileSync(file, "utf-8");
},
},
},
},
},
});
参考:
- Vue 3.3发布公告
- 导入类型 (Vue 3.3功能) 不起作用 (一旦此问题关闭,应删除此解决方法)
英文:
It's not long since Vue 3.3 is dropped and as of now, Nuxt doesn't have built-in support for Vue 3.3's "Imported Types". There's a workaround for this though:
- If you don't have
typescript
inpackage.json
, add itnpm i -D typescript
(required) - Add following code in your
nuxt.config.ts
:
import { existsSync, readFileSync } from "node:fs";
export default defineNuxtConfig({
vite: {
vue: {
script: {
fs: {
fileExists(file: string) {
return existsSync(file);
},
readFile(file: string) {
return readFileSync(file, "utf-8");
},
},
},
},
},
});
References:
- Announcing Vue 3.3
- Imported types (vue 3.3 feature) not working (Once this issue gets closed, you should remove the workaround)
答案2
得分: 0
"vite"在nuxt.config.ts中的配置不再必要。只需安装typescript包即可。
英文:
The config for vite in the nuxt.config.ts isn't necessary anymore. It is enough to install the typescript package.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论