Last active
July 10, 2024 10:43
-
-
Save LearningNerd/08a5039a7c5cddf7342ecd0a32da94e3 to your computer and use it in GitHub Desktop.
Using import/export for front-end modules, example 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function helper(x) { | |
console.log(x); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<!-- ..... other <head> stuff here .... --> | |
<!-- *** IMPORTANT: use type="module" here! *** --> | |
<script type="module" src="main.js"></script> | |
</head> | |
<body> | |
<!-- ... <body> stuff here ... --> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IMPORTANT: | |
// - Must preface relative file paths with / or ./ etc | |
// - Must include the .js at end of the file name | |
// - Must surround named imports with { } | |
import {helper} from "./helper.js"; | |
helper("This works!"); |
Hey @wStrike18! I believe you need to run a local web server to test your code if you're using modules -- see this article from MDN: https://developer.mozilla.org/en-US/docs/Learn/Common_questions/set_up_a_local_testing_server
In javascript front-end, module imported must end with .js in path. Overwise it will not work
Thanks for the example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, when i do that i get a CORS error. you know why ?
index.html
main.js
transaccion.js
main.js
import { Transaccion } from "./transaccion.js"
Access to script at 'file:///C:/nodejs/gestor-de-gastos/frontend/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.