亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

一行命令, 靜態json變身api

標簽:
Java

作为一个前端开发者, 你可以会遇到没有测试数据的尴尬, 而这次我们用json-server, 优雅的解决这个问题

700

效果


关于 json-server

700

json-server


全局安装方式:

npm install -g json-server

使用方式: 如果有一个名为douyu.json的文件, 它的启动命令为

json-server --watch douyu.json


700

启动界面


json-server最外层json的值, 默认只支持数组对象


使用axios发出请求

axios.get("http://localhost:3000/data")
.then(res=> {    return (res.data.rl)
})

效果测试

动图效果

核心代码:

vue组件化代码

<template>
    <div class="source-atom">
        <div v-for="anchorInfo in anchorsInfo" class="atom">
            <img  v-bind:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="anchorInfo['image']" height="200" width="200" alt="" v-bind:title="message">
            <section>
                <div class="title">{{ anchorInfo['name'] }}</div>
                <article>{{ anchorInfo['desc'] }}</article>
            </section>
        </div>
    </div></template><script>import axios from 'axios';export default {    data: function() {        return {            message: "斗鱼主播",            anchorsInfo: this.getPersonImageAddr()
        }
    },    methods: {        getPersonImageAddr: function(){            let anchorsInfo = [];

            axios.get("http://localhost:3000/data")
            .then(res=> {                return (res.data.rl)
            })
            .then(res=>{                let anchorInfo = []                for (let i = 0; i < res.length; i++){                    let anchorInfo = {};
                    anchorInfo["name"] = res[i]['nn'];
                    anchorInfo["image"] = res[i]['rs1'];
                    anchorInfo["desc"] = res[i]['rn'];
                    anchorsInfo.push(anchorInfo)
                }                return anchorsInfo;
            })            return anchorsInfo
        }
    }
}</script><style scoped lang="less">
    .source-atom {        display: flex;        flex-wrap: wrap;        justify-content: center;
    }    .atom{        float: left;        margin: 10px;        padding: 10px;        border: 1px solid #BDBDBD;

    }    .title {        font-size: 20px;        color: #A84631
    }    
</style>

数据来源:

https://www.douyu.com/gapi/rkc/directory/2_201/1




點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
3868
獲贊與收藏
283

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消