TS7016: Could not find a declaration file for module ‘vue-router‘.解决办法

前端初始化的时候出现这样一个错误:

typescript 复制代码
ERROR in src/router/index.ts:1:64                                                                                                                                    
TS7016: Could not find a declaration file for module 'vue-router'. './node_modules/vue-router/index.js' implicitly has an 'any' type.
  Try npm i --save-dev @types/vue-router if it exists or add a new declaration (.d.ts) file containing declare module 'vue-router';
  > 1 | import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
      |                                                                ^^^^^^^^^^^^
    2 | import HomeView from "../views/HomeView.vue";

我尝试了以下方法:

typescript 复制代码
# 使用 PowerShell
Remove-Item -Recurse -Force node_modules
Remove-Item package-lock.json

# 清理 npm 缓存
npm cache clean --force

# 重新安装
npm install

重新运行package.jason文件的serve仍然不行;

再次尝试新建 src/shims-vue-router.d.ts文件:

typescript 复制代码
declare module 'vue-router' {
  import type { App, Component } from 'vue'

  export interface RouteRecordRaw {
    path: string
    name?: string | symbol
    component?: Component
    components?: Record<string, Component>
    redirect?: string
    alias?: string | string[]
    children?: RouteRecordRaw[]
    meta?: Record<string, any>
    beforeEnter?: NavigationGuard | NavigationGuard[]
    props?: boolean | Record<string, any> | ((to: any) => Record<string, any>)
  }

  export interface Router {
    currentRoute: any
    push(to: any): Promise<any>
    replace(to: any): Promise<any>
    go(delta: number): void
    back(): void
    forward(): void
    beforeEach(guard: NavigationGuard): () => void
    afterEach(guard: NavigationHookAfter): () => void
    install(app: App): void  // 添加这行,让 Router 可以作为 Vue 插件使用
  }

  export interface NavigationGuard {
    (to: any, from: any, next: any): any
  }

  export interface NavigationHookAfter {
    (to: any, from: any): any
  }

  export function createRouter(options: any): Router
  export function createWebHistory(base?: string): any
  export function createWebHashHistory(base?: string): any
  export function createMemoryHistory(base?: string): any
  export function useRouter(): Router
  export function useRoute(): any
}

这下可以了:

相关推荐
竹林8181 小时前
在Web3前端用Node.js子进程批量校验钱包,我踩了这些性能与安全的坑
javascript·node.js
农夫山泉不太甜1 小时前
Tauri v2 实战代码示例
前端
yuhaiqiang1 小时前
被 AI 忽悠后,开始怀念搜索引擎了?
前端·后端·面试
红色石头本尊1 小时前
1-umi-前端工程化搭建
前端
真夜2 小时前
关于对echart盒子设置百分比读取的宽高没有撑开盒子解决方案
前端
楠木6852 小时前
RAG 资料库 Demo 完整开发流程
前端·ai编程
肠胃炎2 小时前
挂载方式部署项目
服务器·前端·nginx
像我这样帅的人丶你还2 小时前
使用 Next.js + Prisma + MySQL 开发全栈项目
前端
FPGA小迷弟2 小时前
FPGA 时序约束基础:从时钟定义到输入输出延迟的完整设置
前端·学习·fpga开发·verilog·fpga
Kel2 小时前
深入剖析 openai-node 源码:一个工业级 TypeScript SDK 的架构之美
javascript·人工智能·架构