Javascript module not working in browser?

前端 未结 8 1891
半阙折子戏
半阙折子戏 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:13

    If you're using webpack and babel and want to import the code into your bundle, I guess it should be one of the following:

    export default function show_message(){
        alert("Hello");
    }
    

    and then in your code:

    import show_message from 'path/to/show_message.js'
    // or 
    import { default as someOtherName } from 'path/to/show_message.js'
    

    Or if you'd like to export several functions:

    const show_message = function(){
        alert("Hello");
    }
    export { show_message };
    

    and then in your code:

    import { show_message } from 'path/to/show_message.js'
    // or 
    import { show_message as someOtherName } from 'path/to/show_message.js'
    

    Hope that helps.

提交回复
热议问题