vue多頁面項(xiàng)目中路由使用history模式的方法
前言
之前寫了一個(gè)vue項(xiàng)目中需要添加一個(gè)打印的頁面,需要使用多頁面的模式進(jìn)行開發(fā),vue-cli3出初始化的項(xiàng)目配置多頁面還是很容易的,但是發(fā)現(xiàn)print.html沒有辦法配置history模式的路由,一旦使用history模式的路由。寫了一個(gè)簡單的demo在網(wǎng)上尋求幫助沒有能解決問題,后來沒有辦法只能使用hash模式完整項(xiàng)目了。
如何解決
有一天看webpack文檔的時(shí)候,突然看到了historyApiFallback
配置項(xiàng),一瞬間感覺找到方法了。下班后回家就下載下之前的項(xiàng)目折騰了。
之前的vue.config.js中的配置是這樣的,
const path = require('path') function resolve (dir) { return path.join(__dirname, dir) } module.exports = { pages: { index: { entry: 'src/main.js', template: 'public/index.html', filename: 'index.html', title: 'Index Page', }, print: { entry: 'src/print/main.js', template: 'public/print.html', filename: 'print.html', title: 'print Page', } }, chainWebpack: config => { config.resolve.alias .set('@', resolve('src')) .set('assets',resolve('src/assets')) .set('components',resolve('src/components')); } }
然后根據(jù) webpack文檔 ,添加了如下代碼:
configureWebpack: { devServer: { historyApiFallback: { verbose: true, rewrites: [ { from: /^\/index\/.*$/, to: '/index.html'}, {from: /^\/print\/.*$/, to: '/print.html'} ] } } }
接下來啟動(dòng)項(xiàng)目去瀏覽器中驗(yàn)證,發(fā)現(xiàn)訪問 localhost:8080/print/hello
和 localhost:8080/index/hello-world
能夠分別訪問到 print.html 和 index.html 頁面。但是不能進(jìn)入對應(yīng)的路由于是修改各自的路由文件,修改完后的路由分別為:
// print import HelloWold from '../components/HelloWorld' import goBack from '../components/GoBack' export default [ { path: '/print/hello', name: 'print', component: HelloWold }, { path: '/print/go-back', name: 'print', component: goBack } ] // index import HelloWold from '../components/HelloWorld.vue' export default [ { path: '/index/hello-world', name: 'hello-world', component: HelloWold } ]
在瀏覽器中訪問,可以了~~~
項(xiàng)目地址 github
總結(jié)
以上所述是小編給大家介紹的vue多頁面項(xiàng)目中路由使用history模式的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Vue3+echarts5踩坑以及resize方法報(bào)錯(cuò)的解決
這篇文章主要介紹了Vue3+echarts5踩坑以及resize方法報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vue中的任務(wù)隊(duì)列和異步更新策略(任務(wù)隊(duì)列,微任務(wù),宏任務(wù))
這篇文章主要介紹了vue中的任務(wù)隊(duì)列和異步更新策略(任務(wù)隊(duì)列,微任務(wù),宏任務(wù)),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式
最近的項(xiàng)目完成后,在性能優(yōu)化階段需要做按鈕的防止重復(fù)點(diǎn)擊功能,本文主要介紹了vue uniapp 防止按鈕多次點(diǎn)擊的三種實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08vue 循環(huán)加載數(shù)據(jù)并獲取第一條記錄的方法
今天小編就為大家分享一篇vue 循環(huán)加載數(shù)據(jù)并獲取第一條記錄的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue.js click點(diǎn)擊事件獲取當(dāng)前元素對象的操作
這篇文章主要介紹了vue.js click點(diǎn)擊事件獲取當(dāng)前元素對象的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08