Introduce router in main.ts
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router/index.js'
// 导入Unocss
import 'uno.css'
createApp(App).use(router).mount('#app')
Error message can't find path
[vite] Internal server error: Failed to resolve import "@/views/LoginView.vue" from "src\router\index.ts". Does the file exist?
Plugin: vite:import-analysis
File: D:/project/vue3-shopping/src/router/index.ts
4 | path: "/login",
5 | name: "Login",
6 | component: () => import("@/views/LoginView.vue"),
solve:
Modify vite.config.ts configuration
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// 引入Unocss
import Unocss from 'unocss/vite';
import { presetUno, presetAttributify, presetIcons } from "unocss"
import path from 'path' // 需安装此模块
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(),
Unocss({ // 使用Unocss
presets: [
presetUno(),
presetAttributify(),
presetIcons()],
})],
resolve: {
alias: { // 这里就是需要配置resolve里的别名
"@": path.join(__dirname, "./src"), // path记得引入
// 'vue': 'vue/dist/vue.esm-bundler.js' // 定义vue的别名,如果使用其他的插件,可能会用到别名
},
}
})
Install the path module
Three ways, the first one I use.
npm install --save-dev @types/node
cnpm install --save-dev @types/node
yarn add @types/node -D
After the configuration is complete, run
npm run dev
Solve it, sprinkle flowers!