寫(xiě)在前面
使用vue cli進(jìn)行開(kāi)發(fā)瓤介,所有表現(xiàn)都會(huì)通過(guò)注入在index.html表現(xiàn)。
如果要實(shí)現(xiàn)多個(gè)頁(yè)面節(jié)點(diǎn)远荠,一般通過(guò)路由來(lái)實(shí)現(xiàn)矮固,路由有兩種模式,分別是默認(rèn)的hash模式譬淳,History模式档址,它們的區(qū)別是:
hash模式,產(chǎn)生的目錄結(jié)構(gòu)為:www.doamin.com/#/good
history模式邻梆,產(chǎn)生的目錄結(jié)構(gòu)為:www.doamin.com/good/
配置的方法是定義router/router.js文件
const router = new Router({
mode: 'history',
routers,
});
即使是上面的方法守伸,但對(duì)傳統(tǒng)網(wǎng)頁(yè)有所愛(ài)好者依然會(huì)不滿意,比如會(huì)網(wǎng)友問(wèn):
當(dāng)前目前如果有多個(gè)頁(yè)面怎么辦浦妄?
比如是否能實(shí)現(xiàn)這樣的網(wǎng)頁(yè)結(jié)構(gòu):
www.z01.com/pub/index.html
www.z01.com/pub/listpage.shtml
答案是有的尼摹,偉大領(lǐng)袖告訴我們自力更生豐衣足食,讓我們動(dòng)起手來(lái)校辩!
1 準(zhǔn)備工作
1.1 先查看版本
npm -V
npm@6.12.0 C:\Program Files\nodejs\node_modules\npm
vue -V
@vue/cli 4.1.2
1.2 創(chuàng)建項(xiàng)目
vue create templates
會(huì)創(chuàng)建一個(gè)名為templates的項(xiàng)目
目錄結(jié)構(gòu)為:
npm run serve
App running at:
- Local: http://localhost:8080/
- Network: http://192.168.43.36:8080/
Note that the development build is not optimized.
To create a production build, run npm run build.
運(yùn)行npm run serve窘问,可以在
http://localhost:8080/
http://192.168.43.36:8080/
看到這個(gè)頁(yè)面:
2 多頁(yè)面配置
2.1 修改目錄
1、在src目錄下創(chuàng)建一個(gè)pages文件夾宜咒,
2惠赫、在pages文件夾下面創(chuàng)建index文件夾,
修改之后文件夾結(jié)構(gòu)為:
3故黑、把public文件夾下的index.html移到index文件夾儿咱,
4、把components文件夾下面的HelloWorld.vue移到index文件夾场晶,(這里可以不移動(dòng)混埠,因?yàn)檫@個(gè)是默認(rèn)生成的一個(gè)例子,實(shí)際開(kāi)發(fā)中诗轻,創(chuàng)建項(xiàng)目生成的這個(gè)頁(yè)面我們根本用不到钳宪,移動(dòng)只是為了方便做例子,不另外寫(xiě)一個(gè)index頁(yè)面)
5扳炬、把src文件夾下的App.vue和main.js都移到index文件夾下面
這時(shí)候目錄結(jié)構(gòu):
2.2 理一下邏輯
index.html
這個(gè)文件便是打開(kāi)首頁(yè)顯示的文件吏颖。
<!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>templates</title>
</head>
<body>
<noscript>
<strong>We're sorry but templates doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div> <!-- 記住這里!:拚痢0胱怼!-->
<!-- built files will be auto injected -->
</body>
</html>
記住 id="app"這行
App.vue
內(nèi)容如下:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'app',
components: {
HelloWorld
}
}
</script>
<style>
#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>
這里就是寫(xiě)index.html中id="app"這個(gè)div的內(nèi)容劝术。
并且引入了HelloWorld.vue
在
components: {
HelloWorld
}
包含了這個(gè)組件
在
<HelloWorld msg="Welcome to Your Vue.js App"/>
給HelloWorld.vue傳入了一個(gè)msg變量
HelloWorld.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1> <!--傳入的msg變量在這里 -->
<p>
For a guide and recipes on how to configure / customize this project,<br>
check out the
<a target="_blank" rel="noopener">vue-cli documentation</a>.
</p>
<h3>Installed CLI Plugins</h3>
<ul>
<li><a target="_blank" rel="noopener">babel</a></li>
<li><a target="_blank" rel="noopener">eslint</a></li>
</ul>
<h3>Essential Links</h3>
<ul>
<li><a target="_blank" rel="noopener">Core Docs</a></li>
<li><a target="_blank" rel="noopener">Forum</a></li>
<li><a target="_blank" rel="noopener">Community Chat</a></li>
<li><a target="_blank" rel="noopener">Twitter</a></li>
<li><a target="_blank" rel="noopener">News</a></li>
</ul>
<h3>Ecosystem</h3>
<ul>
<li><a target="_blank" rel="noopener">vue-router</a></li>
<li><a target="_blank" rel="noopener">vuex</a></li>
<li><a target="_blank" rel="noopener">vue-devtools</a></li>
<li><a target="_blank" rel="noopener">vue-loader</a></li>
<li><a target="_blank" rel="noopener">awesome-vue</a></li>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
render: h => h(App)這句話的意思是創(chuàng)建 App element
就是下面這個(gè)函數(shù)的意思
render: function (createElement) {
return createElement(App);
}
2.3 修改文件名稱(chēng)
App.vue改為index.vue;
main.js改為index.js
2.4 修改文件內(nèi)容
index.vue
<template>
<div id="app">
<img alt="Vue logo" src="../../assets/logo.png"> 修改這里
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
import HelloWorld from './HelloWorld.vue' 修改這里
export default {
name: 'app',
components: {
HelloWorld
}
}
</script>
<style>
#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>
index.js
import Vue from 'vue'
import App from './index.vue' 修改這里
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
3 配置vue.config.js
在src文件夾同一級(jí)目錄添加一個(gè)名為vue.config.js的js文件缩多。
內(nèi)容如下:
module.exports = {
publicPath: '/',
outputDir: 'dist',
assetsDir: 'assets',
indexPath: 'index.html',
filenameHashing:true,
pages: {
index: {
entry: "./src/pages/index/index.js", // 配置入口js文件
template: "./src/pages/index/index.html", // 主頁(yè)面
filename: "index.html", // 打包后的html文件名稱(chēng)
title: "首頁(yè)", // 打包后的.html中<title>標(biāo)簽的文本內(nèi)容
// 在這個(gè)頁(yè)面中包含的塊呆奕,默認(rèn)情況下會(huì)包含
// 提取出來(lái)的通用 chunk 和 vendor chunk。
chunks: ['chunk-vendors', 'chunk-common', 'index']
}
},
}
具體配置請(qǐng)查看官方文檔
啟動(dòng) npm run serve
這時(shí)候可以重新打開(kāi)首頁(yè)衬吆。
我們做到這里就完成了首頁(yè)頁(yè)面配置梁钾。
4 新增一個(gè)頁(yè)面
新增一個(gè)頁(yè)面,隨便取個(gè)名字 baicai
在pages文件夾下新增一個(gè)baicai文件夾
在白菜文件夾下新建幾個(gè)文件
baicai.html
baicai.vue
baicai.js
編輯文件內(nèi)容
baicai.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>baicai</title>
</head>
<body>
<noscript>
<strong>We're sorry but templates 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>
baicai.vue
<template>
<div id="app">
<img alt="Vue logo" src="../../assets/logo.png">
<h1>一顆數(shù)據(jù)小白菜</h1>
</div>
</template>
<script>
export default {
name: 'app',
}
</script>
<style>
#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>
baicai.js
import Vue from 'vue'
import App from './baicai.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
5 配置router
在src下創(chuàng)建router文件夾咆槽,在router文件夾下創(chuàng)建router.js
修改內(nèi)容為:
import Vue from 'vue'
import Router from 'vue-router'
import index from '../pages/index/index.vue';
import baicai from '../pages/baicai/baicai.vue';
Vue.use(Router);
const routers = [
{
path: '/',
redirect: '/index',
component: index,
meta: {requiresAuth: false}
},
{
path: '/index',
name: 'index',
component: index,
meta: { requiresAuth: false },
},
{
path: '/baicai',
name: 'baicai',
component: baicai,
meta: { requiresAuth: false },
},
];
const router = new Router({
mode: 'history',
routers,
});
export default router;
6 定義vue.config.js
如果只增加了前面幾步陈轿,訪問(wèn)是可以,但頁(yè)面不會(huì)生效秦忿,還需要定義全局使該頁(yè)生效麦射。
回到項(xiàng)目的根目錄,打開(kāi)vue.config.js灯谣,增加baicai一段資源引用潜秋,完整代碼如下:
module.exports = {
publicPath: '/',
outputDir: 'dist',
assetsDir: 'assets',
indexPath: 'index.html',
filenameHashing:true,
pages: {
index: {
entry: "./src/pages/index/index.js", // 配置入口js文件
template: "./src/pages/index/index.html", // 主頁(yè)面
filename: "index.html", // 打包后的html文件名稱(chēng)
title: "首頁(yè)", // 打包后的.html中<title>標(biāo)簽的文本內(nèi)容
// 在這個(gè)頁(yè)面中包含的塊,默認(rèn)情況下會(huì)包含
// 提取出來(lái)的通用 chunk 和 vendor chunk胎许。
chunks: ['chunk-vendors', 'chunk-common', 'index']
},
baicai: {//新增的部份
entry: "./src/pages/baicai/baicai.js", // 配置入口js文件
template: "./src/pages/baicai/baicai.html", // 主頁(yè)面
filename: "baicai.html", // 打包后的html文件名稱(chēng)
title: "首頁(yè)", // 打包后的.html中<title>標(biāo)簽的文本內(nèi)容
// 在這個(gè)頁(yè)面中包含的塊峻呛,默認(rèn)情況下會(huì)包含
// 提取出來(lái)的通用 chunk 和 vendor chunk。
chunks: ['chunk-vendors', 'chunk-common', 'baicai']
},
},
}
7 重啟服務(wù)
打開(kāi)http://localhost:8080/baicai
8 首頁(yè)跳轉(zhuǎn)
修改index文件夾下的 index.vue
<template>
<div id="app">
<img alt="Vue logo" src="../../assets/logo.png">
<div>
<a href="baicai.html">跳轉(zhuǎn)到白菜頁(yè)面</a> 修改這里
</div>
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
import HelloWorld from './HelloWorld.vue'
export default {
name: 'app',
components: {
HelloWorld
}
}
</script>
<style>
#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>
重啟服務(wù)辜窑,打開(kāi)http://localhost:8080
點(diǎn)擊跳轉(zhuǎn)到白菜頁(yè)面
就會(huì)跳轉(zhuǎn)到http://localhost:8080/baicai.html
最后你可以運(yùn)行npm run bulid編譯钩述,最后結(jié)構(gòu)如下所示:
9 總結(jié)
手工多頁(yè)面配置其實(shí)就是2個(gè)步驟:
1、配置vue.config.js中的pages
2穆碎、配置router
項(xiàng)目最后實(shí)例可參見(jiàn)github:
https://github.com/zoomla/vue-cli4-multipage