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
}

这下可以了:

相关推荐
雪隐6 小时前
个人电脑玩AI-16让5060 Ti给你打工——5060Ti 16G 跑 MiniMax-Music-3:从下载到 60s 出歌的全流程
前端·人工智能·后端
张元清7 小时前
React useLatest Hook:在异步回调里读到最新状态 (2026)
javascript·react.js
CAD老兵7 小时前
让 AI Coding Agent 直接访问 CAD 文档:GitMCP 实战指南
前端·人工智能·github
程序员鱼皮8 小时前
DeepSeek Harness 最新邪修曝光!被吹成核弹的极简模式,真的夯吗?
前端·后端·ai编程
袅沫8 小时前
Nginx中部署多个前端项目——区分路径
服务器·前端
Asize8 小时前
为什么写代码前要先规划组件树?Next.js + Redis 笔记系统实战
前端·javascript·next.js
meilindehuzi_a8 小时前
Next.js 全栈基础实战:从 SPA、SSR 到 App Router 与 Todo API
开发语言·javascript·ecmascript
用户921080262868 小时前
6. 数据大屏 WebSocket 稳定性优化:心跳检测、断点续传和消息去重
前端
柚yuzumi8 小时前
CSS 定位布局:让元素各就各位
前端·css
光影少年8 小时前
react navite图片加载优化、大图卡顿、缓存策略
前端·react native·react.js