Nahid Nasem
Nahid Nasem

Reputation: 27

How can I use import and export with this simple code?

I want to use the doAlert function in the index.js file. Now, how do I use import and export in this situation?

//index.html
<script type="module" src="index.js"></script>

//index.js
/*'index.js' is an empty file.*/

//doAlert.js
function doAlert() {
    alert('Hello');
}

Upvotes: 0

Views: 50

Answers (1)

domenikk
domenikk

Reputation: 1743

/* index.html */
<script type="module" src="index.js"></script>
<script type="module" src="doAlert.js"></script>

/* doAlert.js */
export function doAlert() {
    alert('Hello');
}

/* index.js */
import { doAlert } from './doAlert.js';
// use it
doAlert();

More info on JS modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Upvotes: 1

Related Questions