知識(shí)回顧
在學(xué)習(xí)Vue-cli之前,我們已經(jīng)學(xué)習(xí)了前端體系这揣、前后端分離的演變、Vue入門(mén)机打、Vue的基本語(yǔ)法片迅、Vue組件、Vue路由芥挣、Axios等內(nèi)容唯蝶。接下來(lái)我們就來(lái)學(xué)習(xí)標(biāo)磚前端化工程Vue-cli。
Vue-cli
什么是Vue-cli鼓蜒?
Vue-cli是官方提供的一個(gè)腳手架工具征字,我們可以利用它快速生成前端化的工程模板,十分方便好用畅厢。
其功能主要有:
- 統(tǒng)一的目錄
- 快速調(diào)試
- 單元測(cè)試
- 在線(xiàn)運(yùn)行
- ......
環(huán)境安裝
使用npm進(jìn)行全局安裝氮昧,如果是首次安裝可能速度會(huì)有點(diǎn)慢。
npm install vue-cli -g
檢測(cè)我們安裝的Vue應(yīng)用咪辱。
vue list
第一個(gè)Vue-cli前端程序
1椎组、新建文件夾vue-cli。
2专筷、創(chuàng)建一個(gè)基于webpack模板的Vue應(yīng)用程序。

vue init webpack myvue
建議初學(xué)前端化工程的朋友除了Project name吮旅、Project description弦聂、Author和Vue build,其他問(wèn)題全部選擇no!
3枪眉、安裝依賴(lài)(一般下載的前端工程是沒(méi)有任何依賴(lài)的),運(yùn)行程序堡纬。
cd myvue #進(jìn)入當(dāng)前項(xiàng)目目錄
npm install #安裝所有依賴(lài)
npm run dev #啟動(dòng)項(xiàng)目
4蒿秦、程序啟動(dòng)成功后,按住Ctrl鍵炮叶,鼠標(biāo)單擊終端中顯示的http://localhost:8080渡处,即可打開(kāi)瀏覽器訪(fǎng)問(wèn)網(wǎng)頁(yè)進(jìn)行測(cè)試。
如上圖所示侣肄,現(xiàn)在我們已經(jīng)創(chuàng)建了一個(gè)標(biāo)準(zhǔn)的前端化工程了稼锅,之后就基于這個(gè)工程進(jìn)行操作了僚纷。
Vue-cli目錄結(jié)構(gòu)分析
Vue-cli工程的目錄結(jié)構(gòu)如下圖所示。
主要目錄文件的作用:
- build和config:webpack配置文件和項(xiàng)目配置文件剩晴。
- node_modules:這個(gè)目錄一般在開(kāi)源項(xiàng)目中都不存在,我們拿到項(xiàng)目的第一步就是安裝所有依賴(lài)(
npm install
)毅整。 - src:項(xiàng)目的源碼目錄(Vue項(xiàng)目和js代碼)绽左。
- static:靜態(tài)資源文件。
- .babelrc:Babel配置文件(ES6語(yǔ)法轉(zhuǎn)換為ES5語(yǔ)法)戏蔑。
- .editorconfig:編輯器配置鲁纠。
- .gitignore:git文件忽略配置。
- .postcssrc.js:CSS相關(guān)的配置文件改含,即導(dǎo)入CSS插件。
- index.html:首頁(yè)骤视,所有的頁(yè)面都是通過(guò)這里跳轉(zhuǎn)的鹃觉,實(shí)際開(kāi)發(fā)是不使用這個(gè)文件的。
- package.json:項(xiàng)目的配置文件祷肯,包括名稱(chēng)粱玲、版本、描述允青、作者、依賴(lài)颠锉、啟動(dòng)腳本等史汗。
src目錄
src就是我們編寫(xiě)前端項(xiàng)目的源目錄,瓷蛙,所有代碼都寫(xiě)在這個(gè)目錄里面。
main.js
main.js是項(xiàng)目的主入口艰猬,所有的程序都是有這樣一個(gè)主入口的。
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// es6語(yǔ)法命贴,導(dǎo)入組件和依賴(lài)食听!
import Vue from 'vue' // vue 依賴(lài)
import App from './App' // 導(dǎo)入組件
Vue.config.productionTip = false // 關(guān)閉瀏覽器控制臺(tái)關(guān)于環(huán)境的提示!
/* eslint-disable no-new */
// vue的核心對(duì)象
new Vue({
el: '#app', // 節(jié)點(diǎn)
components: { App }, // 組件
template: '<App/>' // 模板
})
App.vue
App.vue是配置路由跳轉(zhuǎn)的標(biāo)準(zhǔn)的Vue模板頁(yè)面葬项。
<!-- HTML 代碼模板 -->
<template>
<div id="app">
<!-- 配置路由跳轉(zhuǎn) -->
<router-link to="/hello"></router-link>
<!--顯示跳轉(zhuǎn)頁(yè)面的內(nèi)容-->
<router-view/>
<HelloWorld/>
</div>
</template>
<!--JS 代碼 -->
<script>
// JS 代碼, 導(dǎo)入我們自己寫(xiě)的模塊迹蛤!
import HelloWorld from './components/HelloWorld'
// 導(dǎo)入對(duì)象App,在其他地方導(dǎo)入的話(huà)就可以直接使用了!
export default {
name: 'App',
components: {
HelloWorld // 組件敌蜂!
}
}
</script>
<!--CSS 樣式: 如果沒(méi)有加 scoped 就是全局生效,如果增加了就是當(dāng)前頁(yè)面生效汗贫!-->
<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>
標(biāo)準(zhǔn)的路由
準(zhǔn)備工作
vue-router是Vue的官方路由秸脱,可以和Vue無(wú)縫集成。
1咐蝇、在項(xiàng)目中安裝vue-router巷查。
npm install vue-router --save-dev
2、在模板化工程中導(dǎo)入和使用它旭寿。
//導(dǎo)入我們的路由組件
import VueRouter from 'vue-router'
//顯式的調(diào)用Vue路由
Vue.use(VueRouter)
測(cè)試
1崇败、清空項(xiàng)目中的多余內(nèi)容。
2缩膝、在Components目錄下創(chuàng)建和定義我們自己的組件。
<template>
<div>
<h1>內(nèi)容頁(yè)</h1>
</div>
</template>
<script>
export default {
name: 'Content'
}
</script>
3檀蹋、在router目錄下的index.js中編寫(xiě)安裝路由云芦。
4、編寫(xiě)我們自己的路由組件桌肴。
// 導(dǎo)入Vue
import Vue from 'vue'
// 導(dǎo)入我們的路由組件
import VueRouter from 'vue-router'
// 顯示的調(diào)用Vue路由
Vue.use(VueRouter);
// 導(dǎo)入我們自己寫(xiě)的組件, 不需要增加 .vue 后綴琉历!
import Content from '../components/Content'
import Main from '../components/Main'
// 配置路由
export default new VueRouter({
// 就是我們上周講的
routes: [
// 規(guī)則1 , content 內(nèi)容頁(yè)跳轉(zhuǎn)規(guī)則!
{
path: '/content',
name: 'content',
component: Content
},
// 規(guī)則2
{
path: '/main',
name: 'main',
component: Main
}
]
})
5彪置、在main.js中配置路由蝇恶。
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
// 導(dǎo)入我們的路由規(guī)則, 自動(dòng)識(shí)別 index.js
import router from './router'
/* eslint-disable no-new */
new Vue({
el: '#app',
router, // 掛載路由!這里實(shí)際上相當(dāng)于router:router潘懊,參數(shù)同名可省略
components: { App },
template: '<App/>'
})
6、在App.vue中使用即可授舟。
<template>
<div id="app">
<router-link to="/main">首頁(yè)</router-link>
<router-link to="/content">內(nèi)容</router-link>
<!-- 出口贸辈,展現(xiàn)路由內(nèi)容的地方! -->
<router-view></router-view>
</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>
404配置(擴(kuò)展)
1躏哩、編寫(xiě)404.vue組件揉燃。
<template>
<div>
<h1>頁(yè)面不存在!404</h1>
</div>
</template>
<script>
export default {
name: 'NotFound'
}
</script>
2正驻、在index.js中配置路由弊攘。
//省略export default new VueRouter({routers;[...]})
// 關(guān)于404襟交,路由會(huì)優(yōu)先匹配精準(zhǔn)的伤靠,然后匹配通用!
{
path: '*',//通配宴合,但是會(huì)優(yōu)先匹配N(xiāo)otFound組件
component: NotFound
}
路由模式
Vue-cli工程有兩種路由模式:
- hash:默認(rèn)路由模式,路徑會(huì)帶#贞言,如:http://localhost:8080/#/main
- history:不帶#阀蒂,就是我們常常看到的網(wǎng)頁(yè)路由蚤霞,如:http://localhost:8080/main
可以在index.js中配置路由模式。
export default new VueRouter({
mode: 'history', // 配置路由模式级零!
routes: []
}
配置完成后訪(fǎng)問(wèn)網(wǎng)頁(yè)路徑中就不會(huì)有#了滞乙。