怎么在VUE.CLI4.0中配置多頁面入口?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
實現
使用vue/cli生成一個vue項目
npm install -g @vue/cli
個人不建議直接全局安裝,因為可能會對其他項目造成影響,所以會選擇加上 -D
來進行本地安裝
然后 vue create project-name
(使用本地安裝的記得加上 npx
)
成功創建之后,我們看看當前的目錄結構
這里我們需要重構一下我們的目錄 讓他更可觀
配置vue.config.js
let path = require('path') let glob = require('glob') // 用于篩選文件 // 工廠函數 - 配置pages實現多頁面獲取某文件夾下的html與js function handleEntry(entry) { let entries = {} let entryBaseName = '' let entryPathName = '' let entryTemplate = '' let applicationName = '' glob.sync(entry).forEach(item => { console.log('!!!', item) entryBaseName = path.basename(item, path.extname(item)) console.log('entryBaseName:', entryBaseName) entryTemplate = item.split('/').splice(-3) console.log('entryTemplate:', entryTemplate) entryPathName = entryBaseName // 正確輸出js和html的路徑 console.log('entryPathName', entryPathName) entries[entryPathName] = { entry: 'src/' + entryTemplate[0] + '/' + entryTemplate[1] + '/' + entryTemplate[1] + '.js', template: 'src/' + entryTemplate[0] + '/' + entryTemplate[1] + '/' + entryTemplate[2], title: entryTemplate[2], filename: entryTemplate[2] } }) return entries } let pages = handleEntry('./src/applications/**?/*.html') console.log(pages) // 以下開始配置 module.exports = { lintOnSave: false, // 關掉eslint /** * baseUrl 從 3.3起廢用,使用pubilcPath代替 * 默認情況下,Vue CLI 會假設你的應用是被部署在一個域名的根路徑上,例如 https://www.my-app.com/。如果應用被部署在一個子路徑上,你就需要用這個選項指定這個子路徑。例如,如果你的應用被部署在 https://www.my-app.com/my-app/,則設置 publicPath 為 /my-app/。 * 這個值也可以被設置為空字符串 ('') 或是相對路徑 ('./'),這樣所有的資源都會被鏈接為相對路徑,這樣打出來的包可以被部署在任意路徑,也可以用在類似 Cordova hybrid 應用的文件系統中。 */ publicPath: process.env.NODE_ENV === "production" ? "./" : "/", productionSourceMap: false, // 入口設置 pages, devServer: { index: '/', // 運行時,默認打開application1頁面 // 告訴dev-server在服務器啟動后打開瀏覽器,將其設置true為打開默認瀏覽器 open: true, host: 'localhost', port: 8080, https: false, hotOnly: false, // 配置首頁 入口鏈接 before: app => { app.get('/', (req, res, next) => { for (let i in pages) { res.write(`<a target="_self" href="/${i}">/${i}</a></br>`); } res.end() }); } } }
application1.js import Vue from 'vue' import Application1 from './application1.vue' import router from './router' import store from './vuex' Vue.config.productionTip = false new Vue({ router, store, render: h => h(Application1) }).$mount('#app')
application1.vue <template> <div id="app"> <a class='tips' href='application2.html'> Hello Im Application1,Clike me can go to Application2 </a> </div> </template> <style lang="less"> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; } .tips{ display: flex; justify-content: center; align-items:center; color:lightsalmon; font-size:20px; font-weight:bold; } </style>
application1.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title>Application1</title> </head> <body> <noscript> <strong>We're sorry but test-my-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
同理 application2應用也這樣配置 運行
npm run serve
運行
關于怎么在VUE.CLI4.0中配置多頁面入口問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。