简体   繁体   English

从外部js脚本触发function进入Vue

[英]Trigger function from external js script into Vue

There is a script that is loaded into my Vue app inside the mounted hook, by this way:通过这种方式,在挂钩内有一个脚本加载到我的 Vue 应用程序中:

let recaptchaScript = document.createElement("script");
recaptchaScript.setAttribute("src", "https://sso.****.com.co/v3/****.main.js");
document.body.append(recaptchaScript);

The script is loaded, but there is a function that is injected into the window object.脚本加载了,但是有一个function注入到window object。

Currently when I need to use that function, I do this:目前,当我需要使用 function 时,我会这样做:

mounted() {
  let recaptchaScript = document.createElement("script");
  recaptchaScript.setAttribute("src", process.env.VUE_APP_TAPIT_SSO_URL);
  document.body.append(recaptchaScript);
  this.$nextTick(() => {
    setTimeout(() => {
      window.ssoApp.configApp(TAPIT_SSO_CONFIG);
    }, 1500);
  });

I want to get rid the setTimeOut, but I haven't figured it out yet how to wait for the script executes and inject that object of functions (ssoApp) into the window object without using a timer.我想摆脱 setTimeOut,但我还没有弄清楚如何等待脚本执行并将 object 函数 (ssoApp) 注入 window object 而不使用计时器。 Is there any way to do this?有什么办法吗? I've tried almost everything.我几乎尝试了一切。

The element should have both src and an onload property.该元素应同时具有srconload属性。 Set the source last, so that everything that can be done synchronously is done before starting to load.最后设置源,这样所有可以同步完成的事情都在开始加载之前完成。

mounted() {
  let recaptchaScript = document.createElement("script");
  document.body.append(recaptchaScript);
  recaptchaScript.onload = () => window.ssoApp.configApp(TAPIT_SSO_CONFIG);
  recaptchaScript.src = process.env.VUE_APP_TAPIT_SSO_URL;
}

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

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