[Vuejs]-Inject local script in vue.js app to different components

0👍

This is how I do it in Vue/React, just have this function in a helper and call it from any component on creation:

public injectScript(url, loadAsync=true): HTMLScriptElement {
    const script = document.createElement("script");
    script.type = "text/javascript";
    script.async = loadAsync;
    script.src = url;
    document.head.appendChild(script);
    return script;
}

Ignore the types if you are using JS. You can also use the returned script object to track events:

const script = injectScript("...");
script.onLoad = () => {/*do something*/}

Also, make sure to keep this limited to external widgets. If this is your code, surely there’s a better way.

Leave a comment