创建一个vite项目
git仓库:
https://gitee.com/huang-huangbo/demo.git
每个文件都是代码都是
<script setup>
</script>
<template>
主页
<input type="text" >
<a href="./login">登入</a>
<a href="./register">注册</a>
<a href="./main">主页</a>
<a href="./helloWorld">欢迎</a>
</template>
<style scoped>
</style>
router.js
import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
const routes = [
{
path: '/',
redirect: '/login'
},
{
path: '/login',
name: 'Login',
component: () => import('@/views/login.vue')
},
{
path: '/register',
name: 'Register',
component: () => import('@/views/register.vue')
},
{
path: '/main',
name: 'Main',
component: () => import('@/views/main.vue')
},
{
path: '/helloWorld',
name: 'HelloWorld',
component: () => import('@/components/HelloWorld.vue')
}
]
const router = createRouter({
mode: 'history',
history: createWebHistory(),
routes
})
export default router
//vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
// https://vitejs.dev/config/
export default defineConfig({
base:'./',
publicDir: 'public',
plugins: [vue()],
resolve:{
alias:{
'@': resolve(__dirname, 'src')
}
}
})
根据一下配置,打包dist,放在一个空spring boot项目static下
yml文件配置
spring:
web:
resources:
static-locations: classpath:/static/
然后运行8080端口,根路径重定向到login,正常访问
之后刷新或者跳转全部都是404
要怎么解决,百度了各种方法,说vite.config.js要加base:"./",还有router.js换成mode:"hash",yml文件添加static-locations: classpath:/static/没有任何效果,不知道怎么解决,刷新不404,我已经上传gitee,有兴趣解决的可以下载看看
https://gitee.com/huang-huangbo/demo.git
直接前端分离不就行了,不容易出错,后端只提供接口,前端直接调用,nginx配置下就行