一行命令, 静态json变身api

简介: 作为一个前端开发者, 你可以会遇到没有测试数据的尴尬, 而这次我们用json-server, 优雅的解决这个问题效果关于 json-serverjson-server全局安装方式:npm install -g json-server使用方式: 如果有一个名为douyu.

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

效果

关于 json-server

json-server

全局安装方式:

npm install -g json-server

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

json-server --watch douyu.json

启动界面

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: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: "斗鱼主播",
            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
目录
相关文章
|
5月前
|
资源调度 JavaScript Windows
yarn install命令报错解决办法-warning package-lock.json found.
yarn install命令报错解决办法-warning package-lock.json found.
99 0
|
4月前
|
JavaScript 前端开发 Shell
NPM 自定义 package.json 中 scripts 命令(& 与 && 的区别,cross-env 的使用)
NPM 自定义 package.json 中 scripts 命令(& 与 && 的区别,cross-env 的使用)
38 0
|
1月前
|
JSON Shell Linux
Shell获得Curl命令返回的json值
使用curl命令,获取solr中query的结果笔数 Linux中Shell获得json中的值
20 0
|
3月前
|
JSON 关系型数据库 MySQL
这个问题是由于Flink的Table API在处理MySQL数据时,将MULTISET类型的字段转换为了JSON格式
【1月更文挑战第17天】【1月更文挑战第84篇】这个问题是由于Flink的Table API在处理MySQL数据时,将MULTISET类型的字段转换为了JSON格式
34 1
|
3月前
|
JSON Java API
Spring Boot 无侵入式 实现API接口统一JSON格式返回
Spring Boot 无侵入式 实现API接口统一JSON格式返回
|
4月前
|
小程序 API 定位技术
【uni-app报错】选择地址:fail the api need to be declared in the requiredPrivateInfos field in app/ext.json
【uni-app报错】选择地址:fail the api need to be declared in the requiredPrivateInfos field in app/ext.json
110 0
|
4月前
|
XML JSON API
Json实现根据关键词搜索请求唯品会商品列表数据方法,唯品会商品列表数据接口,唯品会API接口申请指南,支持全站
Json实现根据关键词搜索请求唯品会商品列表数据方法,唯品会商品列表数据接口,唯品会API接口申请指南,支持全站
114 1
|
4月前
|
JSON API 数据格式
Json实现根据关键词搜索请求1688商品列表数据方法,1688商品列表数据接口,1688API接口申请指南
Json实现根据关键词搜索请求1688商品列表数据方法,1688商品列表数据接口,1688API接口申请指南
117 0
|
4月前
|
JSON 开发框架 .NET
ASP.NET Core Web API设置响应输出的Json数据格式的两种方式
ASP.NET Core Web API设置响应输出的Json数据格式的两种方式
|
6月前
|
XML JSON 缓存
Json实现根据关键词搜索请求京东商品列表数据方法,京东商品列表数据接口,京东API接口申请指南
Json实现根据关键词搜索请求京东商品列表数据方法,京东商品列表数据接口,京东API接口申请指南
50 0