Injecting into <head> in Vue.js

前端 未结 4 2072
情歌与酒
情歌与酒 2021-01-03 22:15

I have a few EXTERNAL scripts that need to be loaded on various pages, such as Google Places Autocomplete, Facebook APIs, etc.

Obviously it does not make sense to lo

4条回答
  •  庸人自扰
    2021-01-03 22:48

    I think you are talking about some external JS which are not part of node-modules and want to retrieve from external source (http://your-external-script) then you can go for dynamic loading of JS script tag. Put this code somewhere like you first landing screen of SPA in before transition event.

    var script = document.createElement('script');
    script.src = "htpps://your-external-script.js";
    document.head.appendChild(script); //or something of the likes
    

    This will make your external file available in global scope and then you can use it anywhere.

    Note: this scenario is where you dont haev node-moduels for library or you dont want to put as load modules

提交回复
热议问题