Vue處理循環(huán)數(shù)據(jù)流程示例精講
以往我寫前端頁(yè)面的時(shí)候都是使用theamleaf模板引擎,模板引擎的原理其實(shí)就是服務(wù)端進(jìn)行頁(yè)面的渲染,這里需要說(shuō)一下,渲染的意思實(shí)際上就是將相應(yīng)的數(shù)據(jù)變成html標(biāo)簽,比如我們使用了 th:each 標(biāo)簽,那么在服務(wù)端,就會(huì)用for循環(huán)將數(shù)據(jù)裝填成html代碼。
那么我們也可以使用vue框架進(jìn)行頁(yè)面渲染,使用vue進(jìn)行渲染的話,后端只需要將json字符串傳給前端,然后前端拿到數(shù)據(jù)后在瀏覽器端進(jìn)行渲染,實(shí)際上就是使用了客戶端的資源進(jìn)行頁(yè)面渲染,這樣子做可以減輕服務(wù)端的壓力。當(dāng)請(qǐng)求量很小的時(shí)候看不出來(lái)減輕了什么壓力,但是當(dāng)請(qǐng)求量很大的時(shí)候,就能節(jié)省很多計(jì)算資源。
使用vue還有一個(gè)好處就是可以采用前后端分離的方式開發(fā),前端只需要關(guān)心后端傳什么樣格式的數(shù)據(jù)就行了。在使用了vue后感覺(jué)確實(shí)會(huì)比theamleaf的方式好一點(diǎn),至少代碼清爽一點(diǎn)。
下面就展示使用vue處理循環(huán)的一個(gè)例子,首先聲明,這個(gè)例子使用的是vue3的語(yǔ)法,vue3和vue2的語(yǔ)法稍微有億點(diǎn)點(diǎn)不同,使用的時(shí)候需要注意一下。
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <html lang="en"> <head> <meta charset="UTF-8"> <title>納米搜索</title> <link rel="stylesheet" rel="external nofollow" > <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> <script src="https://unpkg.com/vue@3"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div class="container"> <!-- 先編寫一個(gè)搜索欄 --> <div class="row"> <div class="col-md-1"></div> <div class="col-md-10"> <!-- 這里面有兩個(gè)個(gè)部分 --> <div class="row"> <!--<div class="col-md-2"></div>--> <div class="col-md-12"> <div style="float: left; margin-top: 20px;margin-left: 20%"> <h2 style="color:cornflowerblue">納米搜索</h2> </div> <div style="float: left; margin-top: 20px; margin-left: 20px"> <form class="form-inline" action="/search111" method="post"> <div class="form-group" style="margin-right: 20px" > <div class="input-group" > <input type="text" class="form-control" name="keyword" id="keyword" placeholder="請(qǐng)輸入要搜索的關(guān)鍵詞"> </div> </div> <button id="button111" type="submit" class="btn btn-primary" >搜索</button> </form> </div> </div> <!--<div class="col-md-2"></div>--> </div> <hr> <div id="app"> <div v-for="item of msg"> <!-- 第一行是url --> <a :href="item.url" rel="external nofollow" target="_blank"> <div style="color: #0000cc">{{item.title}}</div> </a> <div style="color: #28a745">{{item.url}}</div> <!-- 這一行顯示文章作者 --> <div style="color: #000000">文章作者:<span style="color: #000000; margin-left: 10px">{{item.author_name}}</span></div> <!-- 這一行顯示標(biāo)簽 --> <div style="color: #000000">文章標(biāo)簽:<span style="color: #000000; margin-left: 10px">{{item.tag}}</span></div> <!-- 下面一行顯示發(fā)表時(shí)間,閱讀數(shù)和收藏?cái)?shù) --> <div> <div style="color: #000000">發(fā)表時(shí)間:<span style="color: #000000;margin-left: 10px">{{item.up_time}}</span></div> <div style="color: #000000;float: left">閱讀量:<span style="color: #000000;margin-left: 10px">{{item.read_volum}}</span></div> <div style="color: #000000;float: left; margin-left: 10px">收藏量:<span style="color: #000000;margin-left: 10px">{{item.collection_volum}}</span></div> </div> <br> <hr> </div> </div> </div> <div class="col-md-1"></div> </div> </div> </body> <script type="text/javascript"> Vue.createApp({ data() { return { msg : [{ "author_name": "weixin_68829137", "collection_volum": 31, "read_volum": 747, "tag": "spring java ", "title": "Spring事務(wù)詳解", "u_id": 50, "up_time": "2023-03-21", "url": "https://blog.csdn.net/weixin_68829137/article/details/129687454" }, { "author_name": "chenxiong103", "collection_volum": 5, "read_volum": 4605, "tag": "javascript LayUI ", "title": "layui.table動(dòng)態(tài)獲取表頭和列表數(shù)據(jù)示例", "u_id": 115, "up_time": "2020-07-11", "url": "https://blog.csdn.net/chenxiong103/article/details/107290133" }, { "author_name": "qq_36785719", "collection_volum": 2, "read_volum": 7151, "tag": "JavaScript 前端 ", "title": "layui實(shí)現(xiàn)表格行拖拽,列拖拽等表格操作 -----layui-soul-able", "u_id": 111, "up_time": "2020-07-03", "url": "https://blog.csdn.net/qq_36785719/article/details/107101554" }, { "author_name": "weixin_42334518", "collection_volum": 2, "read_volum": 2838, "tag": "java ", "title": "layui的使用,layui的soulTable的史詩(shī)級(jí)坑", "u_id": 108, "up_time": "2020-10-15", "url": "https://blog.csdn.net/weixin_42334518/article/details/109093503" }, { "author_name": "u013733643", "collection_volum": 3, "read_volum": 1133, "tag": "javascript html ", "title": "layui數(shù)據(jù)表格實(shí)現(xiàn)重載數(shù)據(jù)表格功能(搜索功能)", "u_id": 114, "up_time": "2023-01-30", "url": "https://blog.csdn.net/u013733643/article/details/128806705" }, { "author_name": "weixin_45477086", "collection_volum": 1, "read_volum": 1241, "tag": "spring java ", "title": "Spring事務(wù)詳解與使用", "u_id": 69, "up_time": "2022-05-05", "url": "https://blog.csdn.net/weixin_45477086/article/details/122234635" }], code : 200 } } }).mount("#app"); </script> </html>
上面的例子中已經(jīng)將數(shù)據(jù)都放到代碼里面了,用來(lái)模擬后端向前端發(fā)送數(shù)據(jù)。
需要注意的一點(diǎn)是,我們的js代碼需要寫在待渲染的html代碼后面,因?yàn)闉g覽器是從上到下讀代碼的,如果你將js代碼寫在前面,那么就會(huì)找不到待綁定html標(biāo)簽。
上述代碼可以直接在瀏覽器中運(yùn)行,你可以結(jié)合運(yùn)行結(jié)果來(lái)理解vue的簡(jiǎn)單使用方法。
到此這篇關(guān)于Vue處理循環(huán)數(shù)據(jù)流程示例精講的文章就介紹到這了,更多相關(guān)Vue處理循環(huán)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)下拉加載其實(shí)沒(méi)那么復(fù)雜
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)下拉加載的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue.js中引入vuex儲(chǔ)存接口數(shù)據(jù)及調(diào)用的詳細(xì)流程
這篇文章主要給大家介紹了關(guān)于在vue.js中引入vuex儲(chǔ)存接口數(shù)據(jù)及調(diào)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12vue-resouce設(shè)置請(qǐng)求頭的三種方法
本篇文章主要介紹了vue-resouce設(shè)置請(qǐng)求頭的三種方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09element?ui?From表單校驗(yàn)不生效問(wèn)題解決
表單校驗(yàn)是注冊(cè)環(huán)節(jié)中必不可少的操作,表單校驗(yàn)可以提醒用戶填寫數(shù)據(jù)規(guī)則以確保用戶提交數(shù)據(jù)的有效性,本文主要介紹了element?ui?From表單校驗(yàn)不生效問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01關(guān)于Element-ui中el-table出現(xiàn)的表格錯(cuò)位問(wèn)題解決
使用ElementUI的el-table后,偶然發(fā)現(xiàn)出現(xiàn)行列錯(cuò)位、對(duì)不齊問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Element-ui中el-table出現(xiàn)的表格錯(cuò)位問(wèn)題解決的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07vue緩存的keepalive頁(yè)面刷新數(shù)據(jù)的方法
這篇文章主要介紹了vue緩存的keepalive頁(yè)面刷新數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04keep-Alive搭配vue-router實(shí)現(xiàn)緩存頁(yè)面效果的示例代碼
這篇文章主要介紹了keep-Alive搭配vue-router實(shí)現(xiàn)緩存頁(yè)面效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06