question
Today I encountered the problem that the default hash of the vue project to deploy to the server is fine but the hhistory is blank.
I've studied it and found the answer.
vue project history pattern deployed in subpath
After the project is packaged, it can only be deployed on the server root path by default. If you want to/demo/
This form
vue3+vite configuration method
- exist
Configuration in:
export default defineConfig(({ command }) => { return { // Add base write subpath here base: '/demo/', resolve: { /*...Omitted*/ } }; });
- Then in
router
Added in:
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/' const router = createRouter({ // Pass parameters to createWebHistory method to configure subpath history: createWebHistory(.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView }, ] }) export default router
vue2+cli configuration method
existConfiguration in:
// const vueConfig = { // Add publicPath write subpath here publicPath:'/demo/' //...Ignore other} = vueConfig
Then inrouter
Added in:
export default new Router({ mode: 'history', // Add bese information base: .BASE_URL, scrollBehavior: () => ({ y: 0 }), routes })
Vue project hash mode deploymentOn any path
vue3+vite configuration method
- Bundle
In-house
base
The configuration value is empty or./
:
// ...Other codes are omittedexport default defineConfig(({ command }) => { return { base: '', //base: './', }; });
- Bundle
src/router/
Middle routehistory
Change the mode tohash
model:
import { createRouter, createWebHashHistory } from 'vue-router'; // ...Other codes are omittedconst router = createRouter({ routes, history: createWebHashHistory() });
vue2+vueCli configuration method
- exist
Configuration is empty or
./
:
// const vueConfig = { // Add publicPath write subpath here publicPath:'./' //...Ignore other} = vueConfig
Then inrouter
Set hash in:
export default new Router({ mode: 'hash', scrollBehavior: () => ({ y: 0 }), routes })
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.