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
}

这下可以了:

相关推荐
仰望星空@脚踏实地2 小时前
DataKit js-yaml和follow-redirects组件依赖影响分析
前端·datakit
RustFS2 小时前
RustFS 如何实现对象存储的前端直传?
vue.js·docker·rust
用户4672695597612 小时前
vue 表格 vxe-table 树结构实现单元格复制粘贴功能,实现树层级节点复制功能
vue.js
Mr_fang719402 小时前
iframe 导致 Vue Router go(-1) 无法正常返回问题解决方案
前端
Drift_Dream2 小时前
Node.js 第二课:用核心模块构建你的第一个服务器
前端·后端
DEMO派2 小时前
首页图片懒加载实现方案解析
前端
用户952081611793 小时前
百度地图MapVThree Editor:地图编辑
前端
程序员龙语3 小时前
CSS 文本样式与阴影属性
前端·css
LYFlied3 小时前
【每日算法】LeetCode 152. 乘积最大子数组(动态规划)
前端·算法·leetcode·动态规划