vue官方提供的命令行工具vue-cli,能夠快速搭建單頁應用。默認一個頁面入口index.html,那么,如果我們需要多頁面該如何配置,實際上也不復雜
假設要新建的頁面是rule,以下以rule為例
創建新的html頁面
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <title></title> </head> <body> <span ><div ></div></span> <!-- built files will be auto injected --> </body></html>
創建入口文件Rule.vue和rule.js,仿照App.vue和main.js
<template> <div > <router-view></router-view> </div></template><script> export default { name: 'lottery', data() { return { } }, mounted: function() { this.$router.replace({ path:'/rule' }); }, }</script><style lang="less"> body{ margin:0; padding:0; }</style>
rule.js
import Vue from 'vue'import Rule from './Rule.vue'import router from './router'import $ from 'jquery'//import vConsole from 'vconsole'import fastclick from 'fastclick'Vue.config.productionTip = falsefastclick.attach(document.body)Vue.config.productionTip = false; /* eslint-disable no-new */new Vue({ el: '#rule', router, template: '<Rule/>', components: { Rule },})
修改config/index.js
build添加rule地址,即編譯后生成的rule.html的地址和名字
build: { // Template for index.html index: path.resolve(__dirname, '../dist/index.php'), rule: path.resolve(__dirname, '../dist/rule.php'), …… }
rule: path.resolve(__dirname, '../dist/rule.php')表示編譯后再dist文件下,rule.html編譯后文件名為rule.php
修改build/webpack.base.conf.js
配置entry
entry: { app: './src/main.js', rule: './src/rule.js' },
修改build/webpack.dev.conf.js
在plugins增加
new HtmlWebpackPlugin({ filename: 'rule.html', template: 'rule.html', inject: true, chunks:['rule'] }),
修改build/webpack.prod.conf.js
在plugins增加
new HtmlWebpackPlugin({ filename: config.build.rule, template: 'rule.html', inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true // more options: // https://github.com/kangax/html-minifier#options-quick-reference }, // necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode: 'dependency', chunks: ['manifest','vendor','rule'] }),
新聞熱點
疑難解答