One line command, static json changes to api

As a front-end developer, you may encounter the embarrassment of no test data. This time, we use JSON server to solve this problem gracefully

Effect

about json-server

json-server

Global installation mode:

npm install -g json-server

Usage: if there is a file named douyu.json, its startup command is

json-server --watch douyu.json

Boot interface

json server the value of the outermost json. Only arrays and objects are supported by default

Using axios to make requests

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

Effect test

Motion picture effect

Core code:

vue component code

<template>
    <div class="source-atom">
        <div v-for="anchorInfo in anchorsInfo" class="atom">
            <img  v-bind:src="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: "Fighting fish anchor",
            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>

Data source:

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

Tags: JSON axios npm Vue

Posted on Fri, 20 Mar 2020 14:35:12 -0400 by jaytux