vue-cli 3.x 配置多頁面應用程序(配置文件vue.config.js )

1、在項目src文件夾下,創建一個pages文件夾,用來新建多頁面的各個入口文件

(1)配置index頁面,文件寫在src/pages/index文件夾下,在瀏覽器中訪問地址http://localhost:8090/index.html#/

src/pages/index/index.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>index頁面</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but my-project 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>

src/pages/index/index.js

import Vue from 'vue';
import App from './index.vue';
import router from 'router/indexRoutes.js';

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  router
}).$mount('#app')

src/pages/index/index.vue

<template>
  <div class="index-app">
    <div>vue-cli 3.x 搭建的項目配置多頁面</div> 
    <div>index頁面</div> 
  </div>
</template>

<script>
export default {
  name: 'index-app'
}
</script>

<style>
.index-app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #3d10e0;
  margin-top: 60px;
}
</style>

(2)配置main頁面,文件寫在src/pages/main文件夾下,在瀏覽器中訪問地址http://localhost:8090/main.html#/

src/pages/main/main.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>main頁面</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but my-project 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>

src/pages/main/main.js

import Vue from 'vue';
import App from './main.vue';
import router from 'router/mainRoutes.js';

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  router
}).$mount('#app')

src/pages/main/main.vue

<template>
  <div class="main-app">
    <div>vue-cli 3.x 搭建的項目配置多頁面</div> 
    <div>main頁面</div> 
  </div>
</template>

<script>
export default {
  name: 'main-app'
}
</script>

<style>
.main-app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

2、在vue.config.js文件中設置多頁面入口

const path = require('path');

module.exports = {
    // 配置多頁面入口
    pages: {
        index: {
          // page 的入口
          entry: 'src/pages/index/index.js',
          // 模板來源
          template: 'src/pages/index/index.html',
          // 在 dist/index.html 的輸出
          filename: 'index.html',
          // 當使用 title 選項時,
          // template 中的 title 標籤需要是 <title><%= htmlWebpackPlugin.options.title %></title>
          title: 'Index Page',
          // 在這個頁面中包含的塊,默認情況下會包含
          // 提取出來的通用 chunk 和 vendor chunk。
          chunks: ['chunk-vendors', 'chunk-common', 'index']
        },
        main: {
            // page 的入口
            entry: 'src/pages/main/main.js',
            // 模板來源
            template: 'src/pages/main/main.html',
            // 在 dist/main.html 的輸出
            filename: 'main.html',
            // 當使用 title 選項時,
            // template 中的 title 標籤需要是 <title><%= htmlWebpackPlugin.options.title %></title>
            title: 'Main Page',
            // 在這個頁面中包含的塊,默認情況下會包含
            // 提取出來的通用 chunk 和 vendor chunk。
            chunks: ['chunk-vendors', 'chunk-common', 'main']
        }
    },

    // 環境配置
    devServer: {
        host: '0.0.0.0',
        port: '8090',
        https: false,
        open: true
    },

    // 設置路徑別名
    chainWebpack: config => {
        config.resolve.alias
            .set('@', path.join(__dirname, 'src'))
            .set('assets', path.join(__dirname, 'src/assets'))
            .set('components', path.join(__dirname, 'src/components'))
            .set('request', path.join(__dirname, 'src/request'))
            .set('router', path.join(__dirname, 'src/router'))
            .set('utils', path.join(__dirname, 'src/utils'))
            .set('views', path.join(__dirname, 'src/views'))
    }
};

3、刪除文件public/index.html 、src/App.vue 、src/main.js

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章