Vue cli4 多頁(yè)面共存路由配置全過(guò)程

寫(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)為:


初始結(jié)構(gòu).png
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è)面:


初始index.png

2 多頁(yè)面配置

2.1 修改目錄

1、在src目錄下創(chuàng)建一個(gè)pages文件夾宜咒,
2惠赫、在pages文件夾下面創(chuàng)建index文件夾,
修改之后文件夾結(jié)構(gòu)為:


第一次修改目錄結(jié)構(gòu).jpg

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):


第一次移動(dòng)文件.png
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文件缩多。


添加vue.config.js.png

內(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
新增baicai.jpg

編輯文件內(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

baicai.png

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

跳轉(zhuǎn)到白菜頁(yè)面.png

點(diǎn)擊跳轉(zhuǎn)到白菜頁(yè)面
就會(huì)跳轉(zhuǎn)到http://localhost:8080/baicai.html

一顆數(shù)據(jù)小白菜.png

最后你可以運(yùn)行npm run bulid編譯钩述,最后結(jié)構(gòu)如下所示:


最后發(fā)布出來(lái)的格式

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

注:此文有參照引用http://www.reibang.com/p/3a27c5c4da18

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末牙勘,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子所禀,更是在濱河造成了極大的恐慌方面,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件色徘,死亡現(xiàn)場(chǎng)離奇詭異恭金,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)褂策,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)横腿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人斤寂,你說(shuō)我怎么就攤上這事蔑水。” “怎么了扬蕊?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)丹擎。 經(jīng)常有香客問(wèn)我尾抑,道長(zhǎng)歇父,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任再愈,我火速辦了婚禮榜苫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘翎冲。我一直安慰自己垂睬,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布抗悍。 她就那樣靜靜地躺著驹饺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪缴渊。 梳的紋絲不亂的頭發(fā)上赏壹,一...
    開(kāi)封第一講書(shū)人閱讀 51,482評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音衔沼,去河邊找鬼蝌借。 笑死,一個(gè)胖子當(dāng)著我的面吹牛指蚁,可吹牛的內(nèi)容都是我干的菩佑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼凝化,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼稍坯!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起缘圈,我...
    開(kāi)封第一講書(shū)人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤劣光,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后糟把,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體绢涡,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年遣疯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雄可。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡缠犀,死狀恐怖数苫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情辨液,我是刑警寧澤虐急,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站滔迈,受9級(jí)特大地震影響止吁,放射性物質(zhì)發(fā)生泄漏被辑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一敬惦、第九天 我趴在偏房一處隱蔽的房頂上張望盼理。 院中可真熱鬧,春花似錦俄删、人聲如沸宏怔。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)臊诊。三九已至,卻和暖如春迅矛,著一層夾襖步出監(jiān)牢的瞬間妨猩,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工秽褒, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留壶硅,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓销斟,卻偏偏與公主長(zhǎng)得像庐椒,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蚂踊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354