虽然单页面应用越来越流行,但是多页面应用也有其应用领域,最典型的就是可以避免单页面应用变得过于庞大。
https://cli.vuejs.org/zh/config/#pages
给出了多页面应用的配置方法,只要在vue.config.js中增加配置就可以增加多页面的功能
pages: {
index: {
// entry for the pages
entry: 'src/dashboard/main.js',
// the source template
template: 'src/dashboard/index.html',
// output as dist/index.html
filename: 'index.html',
// when using title option,
// template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
title: 'test'
// chunks to include on this pages, by default includes
// extracted common chunks and vendor chunks.
// chunks: ['chunk-vendors', 'chunk-common', 'index']
},
frontEnd: {
entry: 'src/frontEnd/main.js',
template: 'src/frontEnd/index.html',
filename: 'frontEnd.html',
title: 'test - front'
}
// when using the entry-only string format,
// template is inferred to be `public/subpage.html`
// and falls back to `public/index.html` if not found.
// Output filename is inferred to be `subpage.html`.
// subpage: ''
},
只要通过
http://website.com/index.html
http://website.com/frontEnd/index.html
就可以分别访问这两个不同的页面,如果浏览器和web服务器允许,后面的index.html也可以不输入
要注意的是,这是两个不同的页面,每个页面上的JS脚本运行产生的数据是隔离的,两个页面之间不可以共享数据。vuex也不行
如果一定要共享数据,那么不可以用JS来直接处理,而是要借助其他工具
比如localStorage可以在本地直接共享数据,不过要注意localStorage能存储的数据是十分有限的。
或者也可以利用服务器,比如用户验证的数据就可以通过服务器来共享,具体方法参考cookie和session
如果需要把单页面应用拆分成多页面应用,也就意味着这两个页面之间数据共享量不大,比如说用户登录信息非常小,所以这也不会因为数据传输导致特别严重的的问题。