vue.js 3项目整合vue-router 4的问题

在学习vue.js,通过vue脚手架生成了一个vue项目,名为vue_router_example,带着router配置,代码未做改动

package.json 如下

复制代码
{
  "name": "vue_router_example",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "vue": "^3.2.13",
    "vue-router": "^4.0.3"
  },
  "devDependencies": {
    "@vue/cli-plugin-router": "~5.0.0",
    "@vue/cli-plugin-typescript": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "typescript": "~4.5.5"
  }
}

可知,默认指定的vue-router是4.0.3,启动后报错

复制代码
  App running at:
  - Local:   http://localhost:8080/
  - Network: unavailable

  Note that the development build is not optimized.
  To create a production build, run npm run build.

ERROR in src/router/index.ts:1:64
TS7016: Could not find a declaration file for module 'vue-router'. 'E:/frontend/vue_router_example/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'
    3 |
    4 | const routes: Array<RouteRecordRaw> = [

按照要求执行命令后依旧报错,重新创建一个纯净的vue项目来看一下什么问题,名为vue_example。

package.json 如下

复制代码
{
  "name": "vue_example",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "vue": "^3.2.13"
  },
  "devDependencies": {
    "@vue/cli-plugin-typescript": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "typescript": "~4.5.5"
  }
}

执行命令

复制代码
npm install vue-router@4

package.json 如下

复制代码
{
  "name": "vue_example",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "vue": "^3.2.13",
    "vue-router": "^4.6.3"
  },
  "devDependencies": {
    "@vue/cli-plugin-typescript": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "typescript": "~4.5.5"
  }
}

启动后报错和上面一致,执行删除命令

复制代码
npm uninstall vue-router @types/vue-router

把vue_router_example中的vue-router升高一个版本,安装命令如下

复制代码
npm install vue-router@4.0.4

启动正常,不太理解是怎么回事。

vue.js 3.2.13对应vue-router各个版本的启动情况

|--------------|-------|-------|-------|
| vue-router版本 | 4.0.3 | 4.0.4 | 4.6.3 |
| vue项目启动是否正常 | 否 | 是 | 否 |

查了一下具体什么原因也不清楚,官网上也没有说明的地方,比较奇怪。

相关推荐
不可能片场5 分钟前
Electron 主进程热补丁:单行 JS 安全打法
前端·electron
梨想橙汁12 分钟前
Pinia 状态管理实战:定义 Store、修改数据、模块拆分
前端·vue.js
高申航18 分钟前
Vue 3 + DYMO Connect Framework 实战:从标签模板到打印服务的完整实现
前端·javascript·vue.js
梨想橙汁22 分钟前
Vue3 组件开发:生命周期钩子,组件之间通信全方案
前端·vue.js
雪芽蓝域zzs23 分钟前
分页组件 + el-config-provider 中文国际化 + 靠右布局
前端·javascript·vue.js
恋猫de小郭27 分钟前
iPhone Duo 适配详解,需要改变的不止是布局模型
前端·flutter·ios
萧行之38 分钟前
Observable Plot 源码深度解析——4 核心抽象逐项学习
前端·学习·数据可视化
今日无bug1 小时前
为什么大模型回答总是一个字一个字蹦出来?聊聊 SSE 流式输出与 BFF 层的那些事
前端·llm
志尊宝1 小时前
Vue3 零基础每日笔记(016):v-for 列表渲染——key 的作用与为什么不能用 index
javascript·vue.js·笔记
CIO_Alliance1 小时前
AI微调系列(1)| 全量微调、LoRA、QLoRA 三种方案怎么选
前端·人工智能·神经网络·机器学习·embedding·企业ai转型