vite 请求的模块 vue-router?

Posted

技术标签:

【中文标题】vite 请求的模块 vue-router?【英文标题】:vite The requested module vue-router? 【发布时间】:2021-07-02 09:10:48 【问题描述】:

package.json:


  "name": "blog",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": 
    "dev": "vite --config ./vite.myconfig.js",
    "build": "vue-tsc --noEmit && vite build",
    "serve": "vite preview"
  ,
  "dependencies": 
    "@types/node": "^14.14.37",
    "vue": "^3.0.5",
    "vue-router": "^4.0.4"
  ,
  "devDependencies": 
    "@vitejs/plugin-vue": "^1.2.1",
    "@vue/compiler-sfc": "^3.0.5",
    "typescript": "^4.2.3",
    "vite": "^2.1.5",
    "vue-tsc": "^0.0.15"
  

vite.myconfig.js

import  defineConfig  from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

/**
 * @type import('vite').UserConfig
 */
module.exports = 
  type: 'module',
  plugins: [vue()],
  resolve: 
    alias: 
      '@src': path.resolve(__dirname, './src'),
      '@assets': path.resolve(__dirname, './assets'),
    ,
  ,
  minify: 'terser',

我的路由器脚本

import vueRouter from 'vue-router'

const routes = [
   path: '/home', component:  ,
   path: '/about', component:  ,
]

const router = vueRouter.createRouter(
  history: vueRouter.createWebHistory(),
  routes: routes,
)
export default router

这个错误:

router.ts:1 Uncaught SyntaxError: The requested module '/node_modules/.vite/vue-router.js?v=096e59a1' does not provide an export named 'default'

这是为什么?

【问题讨论】:

【参考方案1】:

正确的语法是import * as vueRouter from 'vue-router';,因为vue路由器不提供默认导出:

import * as vueRouter from 'vue-router';

const routes = [
   path: '/home', component:  ,
   path: '/about', component:  ,
];

const router = vueRouter.createRouter(
  history: vueRouter.createWebHistory(),
  routes: routes,
);
export default router;

import createRouter from 'vue-router';

const routes = [
   path: '/home', component:  ,
   path: '/about', component:  ,
];

const router = createRouter(
  history: vueRouter.createWebHistory(),
  routes: routes,
);
export default router;

【讨论】:

以上是关于vite 请求的模块 vue-router?的主要内容,如果未能解决你的问题,请参考以下文章

vite+vue3+ts中的vue-router基本配置

ts+vue3+vite+pinia+vue-router 踩坑合集

Vite+Vue3+Vue-Router@4+Pinia 快速起步

vite2.0+vue3移动端项目实战

Vite2+vue3+ts 使用 router,layout 搭建页面框架,并做页面自适应

使用vite+vue3构建生产级项目架构