vue2和vue3部署到服務(wù)器子目錄為空白頁問題及解決
問題
今天遇到vue項(xiàng)目部署到服務(wù)器默認(rèn)hash沒問題但是hhistory為空白的問題。
研究了一下找到了答案記錄一下
vue項(xiàng)目history模式部署在子路徑
項(xiàng)目打包后默認(rèn)只能部署在服務(wù)器根路徑,如果想 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項(xiàng)目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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue-cli項(xiàng)目部署到Nginx服務(wù)器的方法
這篇文章主要介紹了Vue-cli項(xiàng)目部署到Nginx服務(wù)器的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11利用Vite2和Vue3實(shí)現(xiàn)網(wǎng)站國際化的全過程
vite2已經(jīng)出來一段時(shí)間了,最近沒忍住嘗試了一下,這篇文章主要給大家介紹了關(guān)于利用Vite2和Vue3實(shí)現(xiàn)網(wǎng)站國際化的相關(guān)資料,需要的朋友可以參考下2021-08-08Vue.js 中取得后臺原生HTML字符串 原樣顯示問題的解決方法
這篇文章主要介紹了VUE.js 中取得后臺原生HTML字符串 原樣顯示問題 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06vue中根據(jù)時(shí)間戳判斷對應(yīng)的時(shí)間(今天 昨天 前天)
這篇文章主要介紹了vue中 根據(jù)時(shí)間戳 判斷對應(yīng)的時(shí)間(今天 昨天 前天),需要的朋友可以參考下2019-12-12Element UI 自定義正則表達(dá)式驗(yàn)證方法
今天小編就為大家分享一篇Element UI 自定義正則表達(dá)式驗(yàn)證方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09element-ui中頁面縮放時(shí)table表格內(nèi)容錯位的解決
這篇文章主要介紹了element-ui中頁面縮放時(shí)table表格內(nèi)容錯位的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08