【问题标题】:Common service file for reactJsreactJs 的通用服务文件
【发布时间】:2021-05-12 05:26:18
【问题描述】:

我想制作一个通用的服务文件结构,用于在 reactJs 中调用外部 api。谁能建议并提供一些示例演示代码?

【问题讨论】:

    标签: javascript reactjs react-redux frontend


    【解决方案1】:
    export async function GetAllAuthorizationElements() {
    
        let response = null;
        await fetch(baseUrl + "/yourroute", {
            headers: defaultHeader
        }).then((response) => response.json()).then(data => response = data);
    
        if (!IsAuthorized(response)) {
            alert("Unauthorized")
            return 403;
        }
    
        return response;
        }
    

    【讨论】:

    • 是否可以与一些演示示例共享一些文件结构?
    • 您只需创建一个文件,例如“DataService.js”,然后将上面的代码粘贴到此文件中。如果您想使用此功能,只需输入“GetAllAuthorizationElements();”在你的 jsx 文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    相关资源
    最近更新 更多