Uncaught SyntaxError: The requested module ‘/node_modules/.vite/deps/vue.js?v=3e1adf4e‘ does not …

9,001次阅读
没有评论

共计 1472 个字符,预计需要花费 4 分钟才能阅读完成。

vue3 导入全局变量时,可以运行但是会一直显示报错,如图:

Uncaught SyntaxError: The requested module‘/node_modules/.vite/deps/vue.js?v=3e1adf4e‘does not ...

 解决方法如下:

在 Vue.js 3 中,”@vue/runtime-core” 是一个包含 Vue.js 运行时核心的模块。这个模块提供了一些关键的 Vue.js 核心类型和方法,例如创建 Vue 实例、虚拟 DOM 渲染、组件渲染等等。当我们在 Vue.js 3 的项目中使用 TypeScript 进行编程时,我们需要使用 ”@vue/runtime-core” 模块来引入这些类型和方法。

在使用 TypeScript 编写 Vue.js 3 项目时,我们通常会使用 “.d.ts” 文件来描述 Vue.js 相关的类型信息。在这些文件中,我们需要声明 ”@vue/runtime-core” 模块的类型信息,以便在我们的项目中使用它时,TypeScript 能够正确地进行类型检查和推断。

为了让 TypeScript 能够识别 ”@vue/runtime-core” 模块中的类型信息,我们需要使用 declare module 语法来声明该模块。具体如下:

// main.ts
// @vue/runtime-core
declare module '@vue/runtime-core' {
    // 在这里添加模块中的类型信息
    interface ComponentCustomProperties{$globalNum2:number}
}

这里需要对 getCurrentInstance() 进行类型断言,还有对 ComponentInternalInstance() 进行类型约束,否则 getCurrentInstance() 函数上的 proxy 对象就会报错。

// HelloWorld.vue
import type {ComponentInternalInstance} from 'vue'

 部分代码如下:

// main.ts
import {createApp} from 'vue'
import './style.css'
import App from './HelloWorld.vue'

const app = createApp(App)
// @vue/runtime-core
declare module '@vue/runtime-core' {
    interface ComponentCustomProperties{$globalNum2:number}
}

app.mount('#app')
// HelloWorld.vue

原文地址: Uncaught SyntaxError: The requested module‘/node_modules/.vite/deps/vue.js?v=3e1adf4e‘does not …

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-11-07发表,共计1472字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)