js import code example

Example 1: alias import javascript

import {default as alias} from 'my-module';

Example 2: javascript import class

//import it
import Example from './file2';
//Create an Instance
var myInstance = new Example()
myInstance.test()

Example 3: js es6 dynamic import default

// something.js

export const hi = (name) => console.log(`Hi, ${name}!`)
export const bye = (name) => console.log(`Bye, ${name}!`)
export default () => console.log('Hello World!')

We can use import() syntax to easily and cleanly load it conditionally:
// other-file.js

if (somethingIsTrue) {
  import('./something.js').then((module) => {
    // Use the module the way you want, as:
    module.hi('Erick') // Named export
    module.bye('Erick') // Named export
    module.default() // Default export
  })
}

Example 4: js import and export

// helloworld.js

export function helloWorld() {
	return 'Hello World!';
}

// main.js

import helloWorld from './helloworld.js';

console.log(helloWorld());

Example 5: javascript import

import { module } from "./path"; // single module
import Module from "./path"; // default export

import Module, { module } from "./path"; // both

Example 6: import all from javascript

import * as module from "./module.js";

Tags:

Html Example