llamar a un archivo javascript en otro archivo usando export o import in javascript

You can use the export keyword to export functions, objects or values from one JavaScript file and then use the import keyword in another file to import and use them.

Here is an example:

In file1.js:

index.tsx
export function myFunction() {
  console.log("Hello from myFunction");
}

export const myValue = 10;

export default class MyClass {
  constructor(name) {
    this.name = name;
  }
}
183 chars
12 lines

In file2.js:

index.tsx
import { myFunction, myValue } from './file1.js';

import MyClass from './file1.js';

// Use the exported function and value
myFunction(); // "Hello from myFunction"
console.log(myValue); // 10

// Use the exported class
const myObject = new MyClass("John");
console.log(myObject.name); // "John"
297 chars
12 lines

Note that the default export is imported without curly braces and can have any name you'd like.

Make sure to specify the correct file path when importing from one file to another.

related categories

gistlibby LogSnag