Javascript module not working in browser?

前端 未结 8 1890
半阙折子戏
半阙折子戏 2020-12-10 11:35

Alright, I have looked on this site and have found several different answers, none of which have worked for me. Basically had a js file that had many functions in it along

8条回答
  •  星月不相逢
    2020-12-10 12:26

    On the script tag you are using to load the js in the browser you need to add the attribute

    type="module"

    It will look like the following:

    
    

    utils.mjs:

    export function addTextToBody(text) {
      const div = document.createElement('div');
      div.textContent = text;
      document.body.appendChild(div);
    }
    

    This is if you are not using a bundler like webpack and working directly in the browser.

    Source of code: https://jakearchibald.com/2017/es-modules-in-browsers/

提交回复
热议问题