简体   繁体   English

为加载 API 添加事件监听器

[英]Add an event listener for the loading of an API


I have a script to execute for elements of an API (named ButterCMS), and I have to wait for the loading of the API to execute this code. 我有一个要为 API(名为 ButterCMS)的元素执行的脚本,我必须等待加载 API 才能执行此代码。 If not, the elements of the API are not yet created and the script return an error. 如果不是,则 API 的元素尚未创建并且脚本返回错误。

The problem is that is can't find how to do that.问题是找不到如何做到这一点。
Here is the code for load the API:这是加载 API 的代码:

const Butter = require('buttercms')
export const butter = Butter('your-api-token')

For this you can load the script  dynamically when your api will return response
after that script tag will be loaded.

fetch("url").then(  
var Script = document.createElement('script');
Script.setAttribute('src','your script url');
document.head.appendChild(Script);
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM