vue2和vue3部署到服務器子目錄為空白頁問題及解決
更新時間:2024年07月05日 14:33:29 作者:我是劉拾貳
這篇文章主要介紹了vue2和vue3部署到服務器子目錄為空白頁問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
問題
今天遇到vue項目部署到服務器默認hash沒問題但是hhistory為空白的問題。
研究了一下找到了答案記錄一下
vue項目history模式部署在子路徑
項目打包后默認只能部署在服務器根路徑,如果想 http://www.xxx.com/demo/
這種形式
vue3+vite配置方法
- 在
vite.config.ts
中配置:
export default defineConfig(({ command }) => { return { // 在這里增加 base 寫子路徑 base: '/demo/', resolve: { /*......省略*/ } }; });
- 然后在
router
中增加:
import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' const router = createRouter({ // 給 createWebHistory 方法傳參數(shù)配置子路徑 history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'home', component: HomeView }, ] }) export default router
vue2+cli配置方法
在 vue.config.ts
中配置:
// vue.config.js const vueConfig = { // 在這里增加 publicPath寫子路徑 publicPath:'/demo/' //......忽略其他 } module.exports = vueConfig
然后在router
中增加:
export default new Router({ mode: 'history', // 增加bese信息 base: process.env.BASE_URL, scrollBehavior: () => ({ y: 0 }), routes })
vue項目hash模式部署在任意路徑
vue3+vite配置方法
- 把
vite.config.ts
中的base
配置值為空或者./
:
// ......省略其它代碼 export default defineConfig(({ command }) => { return { base: '', //base: './', }; });
- 把
src/router/index.ts
中路由history
模式改為hash
模式:
import { createRouter, createWebHashHistory } from 'vue-router'; // ......省略其它代碼 const router = createRouter({ routes, history: createWebHashHistory() });
vue2+vueCli配置方法
- 在
vue.config.ts
中配置為空或者./
:
// vue.config.js const vueConfig = { // 在這里增加 publicPath寫子路徑 publicPath:'./' //......忽略其他 } module.exports = vueConfig
然后在router
中設(shè)置hash:
export default new Router({ mode: 'hash', scrollBehavior: () => ({ y: 0 }), routes })
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用Vite2和Vue3實現(xiàn)網(wǎng)站國際化的全過程
vite2已經(jīng)出來一段時間了,最近沒忍住嘗試了一下,這篇文章主要給大家介紹了關(guān)于利用Vite2和Vue3實現(xiàn)網(wǎng)站國際化的相關(guān)資料,需要的朋友可以參考下2021-08-08Vue.js 中取得后臺原生HTML字符串 原樣顯示問題的解決方法
這篇文章主要介紹了VUE.js 中取得后臺原生HTML字符串 原樣顯示問題 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06vue中根據(jù)時間戳判斷對應的時間(今天 昨天 前天)
這篇文章主要介紹了vue中 根據(jù)時間戳 判斷對應的時間(今天 昨天 前天),需要的朋友可以參考下2019-12-12element-ui中頁面縮放時table表格內(nèi)容錯位的解決
這篇文章主要介紹了element-ui中頁面縮放時table表格內(nèi)容錯位的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08