vue-cli3配置多頁面入口
阿新 • • 發佈:2022-03-08
經常遇到需要配置多入口檔案的需求,做下總結記錄:
第一步:在public檔案下建立一個app.html頁面的檔案。
在專案public資料夾下建立一個app.html,其實就是將index.html複製一份,將title和name改一下:
ps:如果不需要特殊引入其他js,也可以配置好,會自動生成app.vue模板檔案.
第二步:在src資料夾下建立一個app資料夾,分別建立main.js、app.vue兩個檔案,permission檔案是為了許可權控制,可有可無。
三個檔案內容如下:
app下的main.js仿照main.js
// 引入@babel/polyfill處理相容
import "@babel/polyfill";
import Vue from "vue";
import App from "./App.vue";
import router from "./router/";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
Vue.config.productionTip = false;
new Vue({
router,
render: h => h(App)
}).$mount("#app");
app/app.vue(仿造app.vue)
<template> <div id="app">
<router-view></router-view> </div> </template> <script> export default { data(){ return{ } } } </script> <style scoped> </style>
第三步:配置vue.config.js
在module.exports里加上入口配置:
const pages = {
'index': {
entry: 'src/index/main.js',
// 頁面的模板檔案
template: 'public/index.html',
// build 生成的檔名稱 例: dist/index.html
filename: 'index.html',
chunks: ['chunk-libs', 'chunk-commons', 'chunk-elementUI', 'runtime', 'index']
},
'app': {
entry: 'src/app/main.js',
template: 'public/app.html',
filename: 'app.html',
chunks: ['chunk-libs', 'chunk-commons', 'chunk-elementUI', 'runtime', 'app']
}
}
然後執行訪問:localhost:port/app.html/#/即可!!!
打包看看!!!
nginx上這樣配置:
root C:\Users\hoohui_qianduan\Desktop\littleDemo-mianshi\vueAllDemo\dist; location /login { index app.html app.htm; try_files $uri $uri/ /app.html; } location / { index index.html index.htm; try_files $uri $uri/ /index.html; }
vue.config.js中的靜態資源訪問路徑這樣配置:
publicPath: process.env.NODE_ENV === 'production'? '/': '/',//靜態資源訪問路徑
/ :代表從root根路徑訪問 是絕對路徑 靜態資源訪問路徑永遠都是localhost:port/static.....
./ :代表相對路徑 相對於位址列的路徑 假如位址列上是localhost:port/app 那麼靜態資源的訪問路徑就是 localhost:port/app/static.....
/dist/ :也是相對路徑 代表靜態資源路徑在 root的dist資料夾下