2

包.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'

这为什么?

4

1 回答 1

3

正确的语法是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;
于 2021-04-06T14:59:22.150 回答