vue-resourse將json數(shù)據輸出實例
本文主要講v-resourse 獲取json數(shù)據并傳遞到DOM中,具有一定的參考價值,感興趣的小伙伴們可以參考一下
1.demo目錄,不要管index.html和index.js
2.html頁面 vue-resourse-josn1.1.html展示json中的數(shù)據
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>vue-resourse-json</title> </head> <body> <div id="app"> <ul> <li v-for="item in itemList" :id="item.id" style="list-style-type: none;"> 編號:{{item.id}}</br> 作者:{{item.author}}</br> 書名{{item.name}}</br> 價格:{{item.price}}</br> 出版時間{{item.time}}</br> </li> </ul> </div> <script src="static/js/libs/vue.js"></script> <script src="static/js/libs/vue-resource.min.js"></script> <script type="text/javascript" src="static/js/vue-resourse-json.js"></script> </body> </html>
3.js vue-resourse-json.js
var app = new Vue({ el:"#app", data:{ //聲明空數(shù)組,進行數(shù)據接收,最后傳遞到前端頁面 itemList:[], }, //向data數(shù)組里添加數(shù)據 mounted:function(){ this.getData(); }, methods: { getData:function () { var self = this; this.$http.get("static/data/list_json.json").then(function (res) { console.log(res); //var lens = res.body.lists.length; //console.log(lens); //獲取了當前數(shù)組的長度,為3 for(var i= 0,len=res.body.lists.length;i<len;i++){ //已經獲取json數(shù)組中的數(shù)據,接下來如何傳遞到前端頁面中 //獲取全部數(shù)據 var selData = res.body.lists[i]; //console.log(selData); //獲取數(shù)組中的部分數(shù)據 var part = res.body.lists[i].name; //console.log(part); //將獲取的數(shù)據push到空的數(shù)組中itenList, self.itemList.push(selData); } }) } } });
4.json為list_josn.json
下面是json中的數(shù)據
{ "lists":[ { "id":"1", "author":"小華", "name":"《春天來了》", "price":"23", "time":"1998-03-12" }, { "id":"2", "author":"老舍", "name":"《濟南的冬天》", "price":"32", "time":"1956-12-09" }, { "id":"3", "author":"朱自清", "name":"《背影》", "price":"40", "time":"1943-09-12" } ] }
5.結果輸出
6.總結:主要理清數(shù)據請求和傳遞的流程就行了。
本文已被整理到了《Vue.js前端組件學習教程》,歡迎大家學習閱讀。
關于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。
更多vue學習教程請閱讀專題《vue實戰(zhàn)教程》
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue.js遞歸組件實現(xiàn)組織架構樹和選人功能案例分析
這篇文章主要介紹了Vue.js遞歸組件實現(xiàn)組織架構樹和選人功能,本文通過案例代碼講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07vue使用百度地圖報錯BMap?is?not?defined問題及解決
這篇文章主要介紹了vue使用百度地圖報錯BMap?is?not?defined問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10VueJs單頁應用實現(xiàn)微信網頁授權及微信分享功能示例
本篇文章主要介紹了VueJs單頁應用實現(xiàn)微信網頁授權及微信分享功能示例,具有一定的參考價值,有興趣的可以了解一下2017-07-07