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 生成的代码谁来验收?从 Copilot 到 WorkBuddy 的三个实践
前端·ai编程
我不会起名字32227 分钟前
仅 Web 主动 ping:30 秒读超时与各平台保活
前端·后端·websocket·go·即时通讯·im·故障排查
徐小夕40 分钟前
CADViewer-Skill:让 Agent 原生支持 DWG 解析、可视化图统计、图纸导出
前端·vue.js·github
Mh1 小时前
AI 的模型能力越来越强,为什么我的编码能力越来越弱?
前端·javascript·后端
deli0071 小时前
1 万条数据渲染卡成 PPT?虚拟列表把 DOM 从 10000 降到 24
前端
jay神1 小时前
基于 YOLOv8 的传送带异物检测系统(Web 版)
前端·人工智能·深度学习·yolo·计算机视觉·计算机毕业设计
黑土豆1 小时前
网页框一下就能截图Region-snap:我把这套功能封装成了 npm 包,支持 Vue 和 React
前端·javascript·npm
李纲明2 小时前
微信小程序开了 Skyline 后样式全乱、滚动对不上甚至部分组件不能用?Skyline 怎么接才稳
javascript·微信小程序·小程序·devdiy
程序员大阳2 小时前
副队长ES6+教程(7)--ES6+函数新特性
javascript·es6·函数·特性·默认参数·剩余参数·参数解构
zhanlang02172 小时前
HTML 零基础入门到企业实战全套教程【第六章 6.1】表格标签|实现数据可视化展示
前端·信息可视化·html