Vue腳手架 & 生命周期 & 組件化開發
一、今日目標
1.生命周期
- 生命周期介紹
- 生命周期的四個階段
- 生命周期鉤子
- 聲明周期案例
2.綜合案例-小黑記賬清單
- 列表渲染
- 添加/刪除
- 餅圖渲染
3.工程化開發入門
- 工程化開發和腳手架
- 項目運行流程
- 組件化
- 組件注冊
4.綜合案例-小兔仙首頁
- 拆分模塊-局部注冊
- 結構樣式完善
- 拆分組件 – 全局注冊
二、Vue生命周期
思考:什么時候可以發送初始化渲染請求?(越早越好)什么時候可以開始操作dom?(至少dom得渲染出來)
Vue生命周期:就是一個Vue實例從創建 到 銷毀 的整個過程。
生命周期四個階段:① 創建 ② 掛載 ③ 更新 ④ 銷毀
1.創建階段:創建響應式數據
2.掛載階段:渲染模板
3.更新階段:修改數據,更新視圖
4.銷毀階段:銷毀Vue實例
三、Vue生命周期鉤子
Vue生命周期過程中,會自動運行一些函數,被稱為【生命周期鉤子】→ 讓開發者可以在【特定階段】運行自己的代碼
<div id="app"><h3>{{ title }}</h3><div><button @click="count--">-</button><span>{{ count }}</span><button @click="count++">+</button></div></div><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script>const app = new Vue({el: '#app',data: {count: 100,title: '計數器'},// 1. 創建階段(準備數據)// 2. 掛載階段(渲染模板)// 3. 更新階段(修改數據 → 更新視圖)// 4. 卸載階段})</script>
四、生命周期鉤子小案例
1.在created中發送數據
<style>* {margin: 0;padding: 0;list-style: none;}.news {display: flex;height: 120px;width: 600px;margin: 0 auto;padding: 20px 0;cursor: pointer;}.news .left {flex: 1;display: flex;flex-direction: column;justify-content: space-between;padding-right: 10px;}.news .left .title {font-size: 20px;}.news .left .info {color: #999999;}.news .left .info span {margin-right: 20px;}.news .right {width: 160px;height: 120px;}.news .right img {width: 100%;height: 100%;object-fit: cover;}</style><div id="app"><ul><li class="news"><div class="left"><div class="title">5G商用在即,三大運營商營收持續下降</div><div class="info"><span>新京報經濟新聞</span><span>2222-10-28 11:50:28</span></div></div><div class="right"><img src="http://ajax-api.itheima.net/public/images/0.webp" alt=""></div></li><li class="news"><div class="left"><div class="title">5G商用在即,三大運營商營收持續下降</div><div class="info"><span>新京報經濟新聞</span><span>2222-10-28 11:50:28</span></div></div><div class="right"><img src="http://ajax-api.itheima.net/public/images/0.webp" alt=""></div></li><li class="news"><div class="left"><div class="title">5G商用在即,三大運營商營收持續下降</div><div class="info"><span>新京報經濟新聞</span><span>2222-10-28 11:50:28</span></div></div><div class="right"><img src="http://ajax-api.itheima.net/public/images/0.webp" alt=""></div></li></ul></div><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>// 接口地址:http://hmajax.itheima.net/api/news// 請求方式:getconst app = new Vue({el: '#app',data: {list: []}})</script>
2.在mounted中獲取焦點
<style>html,body {height: 100%;}.search-container {position: absolute;top: 30%;left: 50%;transform: translate(-50%, -50%);text-align: center;}.search-container .search-box {display: flex;}.search-container img {margin-bottom: 30px;}.search-container .search-box input {width: 512px;height: 16px;padding: 12px 16px;font-size: 16px;margin: 0;vertical-align: top;outline: 0;box-shadow: none;border-radius: 10px 0 0 10px;border: 2px solid #c4c7ce;background: #fff;color: #222;overflow: hidden;box-sizing: content-box;-webkit-tap-highlight-color: transparent;}.search-container .search-box button {cursor: pointer;width: 112px;height: 44px;line-height: 41px;line-height: 42px;background-color: #ad2a27;border-radius: 0 10px 10px 0;font-size: 17px;box-shadow: none;font-weight: 400;border: 0;outline: 0;letter-spacing: normal;color: white;}body {background: no-repeat center /cover;background-color: #edf0f5;}</style><div class="container" id="app"><div class="search-container"><img src="https://www.itheima.com/images/logo.png" alt=""><div class="search-box"><input type="text" v-model="words" id="inp"><button>搜索一下</button></div></div>
</div><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>const app = new Vue({el: '#app',data: {words: ''}})
</script>
五、案例-小黑記賬清單
1.需求圖示:
2.需求分析
1.基本渲染
2.添加功能
3.刪除功能
4.餅圖渲染
3.思路分析
1.基本渲染
- 立刻發送請求獲取數據 created
- 拿到數據,存到data的響應式數據中
- 結合數據,進行渲染 v-for
- 消費統計 —> 計算屬性
2.添加功能
- 收集表單數據 v-model,使用指令修飾符處理數據
- 給添加按鈕注冊點擊事件,對輸入的內容做非空判斷,發送請求
- 請求成功后,對文本框內容進行清空
- 重新渲染列表
3.刪除功能
- 注冊點擊事件,獲取當前行的id
- 根據id發送刪除請求
- 需要重新渲染
4.餅圖渲染
- 初始化一個餅圖 echarts.init(dom) mounted鉤子中渲染
- 根據數據試試更新餅圖 echarts.setOptions({…})
4.代碼準備
<!-- CSS only --><linkrel="stylesheet"href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"/><style>.red {color: red!important;}.search {width: 300px;margin: 20px 0;}.my-form {display: flex;margin: 20px 0;}.my-form input {flex: 1;margin-right: 20px;}.table > :not(:first-child) {border-top: none;}.contain {display: flex;padding: 10px;}.list-box {flex: 1;padding: 0 30px;}.list-box a {text-decoration: none;}.echarts-box {width: 600px;height: 400px;padding: 30px;margin: 0 auto;border: 1px solid #ccc;}tfoot {font-weight: bold;}@media screen and (max-width: 1000px) {.contain {flex-wrap: wrap;}.list-box {width: 100%;}.echarts-box {margin-top: 30px;}}</style><div id="app"><div class="contain"><!-- 左側列表 --><div class="list-box"><!-- 添加資產 --><form class="my-form"><input type="text" class="form-control" placeholder="消費名稱" /><input type="text" class="form-control" placeholder="消費價格" /><button type="button" class="btn btn-primary">添加賬單</button></form><table class="table table-hover"><thead><tr><th>編號</th><th>消費名稱</th><th>消費價格</th><th>操作</th></tr></thead><tbody><tr><td>1</td><td>帽子</td><td>99.00</td><td><a href="javascript:;">刪除</a></td></tr><tr><td>2</td><td>大衣</td><td class="red">199.00</td><td><a href="javascript:;">刪除</a></td></tr></tbody><tfoot><tr><td colspan="4">消費總計: 298.00</td></tr></tfoot></table></div><!-- 右側圖表 --><div class="echarts-box" id="main"></div></div></div><script src="https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>/*** 接口文檔地址:* https://www.apifox.cn/apidoc/shared-24459455-ebb1-4fdc-8df8-0aff8dc317a8/api-53371058* * 功能需求:* 1. 基本渲染* 2. 添加功能* 3. 刪除功能* 4. 餅圖渲染*/const app = new Vue({el: '#app',data: {},})</script>
六、工程化開發和腳手架
1.開發Vue的兩種方式
- 核心包傳統開發模式:基于html / css / js 文件,直接引入核心包,開發 Vue。
- 工程化開發模式:基于構建工具(例如:webpack)的環境中開發Vue。
工程化開發模式優點:
提高編碼效率,比如使用JS新語法、Less/Sass、Typescript等通過webpack都可以編譯成瀏覽器識別的ES3/ES5/CSS等
工程化開發模式問題:
- webpack配置不簡單
- 雷同的基礎配置
- 缺乏統一的標準
為了解決以上問題,所以我們需要一個工具,生成標準化的配置
2.腳手架Vue CLI
基本介紹:
Vue CLI 是Vue官方提供的一個全局命令工具
可以幫助我們快速創建一個開發Vue項目的標準化基礎架子。【集成了webpack配置】
好處:
- 開箱即用,零配置
- 內置babel等工具
- 標準化的webpack配置
使用步驟:
- 全局安裝(只需安裝一次即可) yarn global add @vue/cli 或者 npm i @vue/cli -g
- 查看vue/cli版本: vue --version
- 創建項目架子:vue create project-name(項目名不能使用中文)
- 啟動項目:yarn serve 或者 npm run serve(命令不固定,找package.json)
七、項目目錄介紹和運行流程
1.項目目錄介紹
雖然腳手架中的文件有很多,目前咱們只需人事三個文件即可
- main.js 入口文件
- App.vue App根組件
- index.html 模板文件
2.運行流程
八、組件化開發
? 組件化:一個頁面可以拆分成一個個組件,每個組件有著自己獨立的結構、樣式、行為。
? 好處:便于維護,利于復用 → 提升開發效率。
? 組件分類:普通組件、根組件。
? 比如:下面這個頁面,可以把所有的代碼都寫在一個頁面中,但是這樣顯得代碼比較混亂,難易維護。咱們可以按模塊進行組件劃分
總結:
組件化的好處是什么?
組件的分類?
九、根組件 App.vue
1.根組件介紹
整個應用最上層的組件,包裹所有普通小組件
2.組件是由三部分構成
- 語法高亮插件
-
三部分構成
- template:結構 (有且只能一個根元素)
- script: js邏輯
- style: 樣式 (可支持less,需要裝包)
-
讓組件支持less
(1) style標簽,lang=“less” 開啟less功能
(2) 裝包: yarn add less less-loader -D 或者npm i less less-loader -D
3.總結
App組件包含哪三部分?
十、普通組件的注冊使用-局部注冊
1.特點:
只能在注冊的組件內使用
2.步驟:
- 創建.vue文件(三個組成部分)
- 在使用的組件內先導入再注冊,最后使用
3.使用方式:
當成html標簽使用即可 <組件名></組件名>
4.注意:
組件名規范 —> 大駝峰命名法, 如 HmHeader
5.語法:
// 導入需要注冊的組件
import 組件對象 from '.vue文件路徑'
import HmHeader from './components/HmHeader'export default { // 局部注冊components: {'組件名': 組件對象,HmHeader:HmHeaer,HmHeader}
}
6.練習
在App組件中,完成以下練習。在App.vue中使用組件的方式完成下面布局
<template><div class="hm-header">我是hm-header</div>
</template><script>
export default {}
</script><style>
.hm-header {height: 100px;line-height: 100px;text-align: center;font-size: 30px;background-color: #8064a2;color: white;
}
</style>
<template><div class="hm-main">我是hm-main</div>
</template><script>
export default {}
</script><style>
.hm-main {height: 400px;line-height: 400px;text-align: center;font-size: 30px;background-color: #f79646;color: white;margin: 20px 0;
}
</style>
<template><div class="hm-footer">我是hm-footer</div>
</template><script>
export default {}
</script><style>
.hm-footer {height: 100px;line-height: 100px;text-align: center;font-size: 30px;background-color: #4f81bd;color: white;
}
</style>
7.總結
- A組件內部注冊的局部組件能在B組件使用嗎
- 局部注冊組件的步驟是什么
- 使用組件時 應該按照什么命名法
十一、普通組件的注冊使用-全局注冊
1.特點:
全局注冊的組件,在項目的任何組件中都能使用
2.步驟
- 創建.vue組件(三個組成部分)
- main.js中進行全局注冊
3.使用方式
當成HTML標簽直接使用
<組件名></組件名>
4.注意
組件名規范 —> 大駝峰命名法, 如 HmHeader
5.語法
Vue.component(‘組件名’, 組件對象)
例:
// 導入需要全局注冊的組件
import HmButton from './components/HmButton'
Vue.component('HmButton', HmButton)
6.練習
在以下3個局部組件中是展示一個通用按鈕
<template><button class="hm-button">通用按鈕</button>
</template><script>
export default {}
</script><style>
.hm-button {height: 50px;line-height: 50px;padding: 0 20px;background-color: #3bae56;border-radius: 5px;color: white;border: none;vertical-align: middle;cursor: pointer;
}
</style>
7.總結
1.全局注冊組件應該在哪個文件中注冊以及語法是什么?
2.全局組件在項目中的任何一個組件中可不可以使用?
十二、綜合案例
1.小兔仙首頁啟動項目演示
2.小兔仙組件拆分示意圖
3.開發思路
-
分析頁面,按模塊拆分組件,搭架子 (局部或全局注冊)
-
根據設計圖,編寫組件 html 結構 css 樣式 (已準備好)
-
拆分封裝通用小組件 (局部或全局注冊)
將來 → 通過 js 動態渲染,實現功能