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项目启动是否正常 | 否 | 是 | 否 |

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

相关推荐
weixin_382395233 小时前
为小工厂量身打造:本地部署的物料管理系统带缺料计算
前端·制造
__zRainy__3 小时前
解决pnpm v10+不自动构建
前端·pnpm·工程化
猫猫不是喵喵.3 小时前
Vue3 Props 属性
前端·javascript·vue.js
醉城夜风~4 小时前
CSS元素显示模式(display)
前端·css
AI大模型-小华5 小时前
Codex 三方充值快速入门指南
java·前端·数据库·chatgpt·ai编程·codex·chatgpt pro
做前端的娜娜子7 小时前
同一链接实现 PC Web 与移动 H5 自适应
前端·掘金·金石计划
小帅不太帅8 小时前
架构没变、规模没变,DeepSeek V4 Flash 正式版凭什么暴涨 47 分?
前端·aigc·deepseek
jarvisuni8 小时前
DeepSeekFlash前端依旧拉垮,而且变慢了很多!
前端·javascript·算法
卷福同学9 小时前
AI编程出海第二步:验证关键词能否做站
前端·人工智能·后端