• miragejs 学习


    miragejs

    - install npm install miragejs --save-dev 一定是这个其他简写方式好像不行
    - 创建初始化文件
    ```
        import { Server } from "miragejs"
    
        export default function ({ environment = 'development'} = {} ){
        return new Server({
            environment,
            models: {
                 reminder: Model,  //配置模型,然后下面可以使用schema
            },
            routes(){
    
                //get 
                this.get('/api/tasks',()=>({
                    tasks:[
                    {id:0, text:'feed the cat'},
                    {id:1, text:'wash the dishes'}
                    ]
                }))
    
                // post 
             this.post("/api/reminders", (schema, request) => {
                let attrs = JSON.parse(request.requestBody)
                console.log(attrs)
                debugger
                })
            }
        })
        } 
    
    
    
    ```
    - 调用文件
    ```
        import createServer from './../src/mirage/server.js'
    
        createServer();
    
        fetch('/api/tasks').then(res=>{
            return res.json();
        }).then(data=>{
            console.log(data)
        })
    
    ```
    
    - seed 设置初始数据
    ```
    seeds(server) {
        server.create("reminder", { text: "Walk the dog" })
        server.create("reminder", { text: "Take out the trash" })
        server.create("reminder", { text: "Work out" })
        },
    ```
    
    - 动态路由
    ```
         this.get("/api/reminders/:id", (schema, request) => {
            debugger;
            console.log(request.params.id)
            return schema.reminders.all()
        })
    ```
    
    - more 
        - 待续
    - reference link 
        - https://css-tricks.com/dont-wait-mock-the-api/
        - https://miragejs.com/tutorial/intro/
        - https://miragejs.com/docs/getting-started/introduction/
    

  • 相关阅读:
    autorun.inf删除方法
    Re_Write序列号
    最常用的正则表达式
    SQL聚合使用GROUP BY
    Ext.Net的Window控件的简单使用
    SQL统计查询一个表中的记录,然后减法运算
    C#金额转换为汉字大写
    Ext.Net的Button按钮的使用
    C# 参考之方法参数关键字:params、ref及out 引用
    C#连接ACCESS 2007数据库
  • 原文地址:https://www.cnblogs.com/cyany/p/13490675.html
Copyright © 2020-2023  润新知