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?的主要内容,如果未能解决你的问题,请参考以下文章
ts+vue3+vite+pinia+vue-router 踩坑合集
Vite+Vue3+Vue-Router@4+Pinia 快速起步