I'm really new to javaScript so I'm a bit stuck on implementing modules in JS.
I have designed a ToDoList page and I'm using it to practice modules. So far, I have been able to export and import all the code where I think it makes sense. However, when trying to use the "import('./).then() syntax the console throws me an error. Please see below:
- The below code works well at the moment.
import { Task, allTasks } from './App/Tasks.js';
function createTask(userInputAccess) {
if(userInputAccess.value.trim() === '') {
alert('You need to add a task!');
return;
};
const newTask = new Task(userInputAccess.value, 'active-list');
userInputAccess.value = '';
allTasks.push(newTask);
document.getElementById(newTask.set).append(newTask.html);
toggleListVisibility(allTasks);
}
const lists = new Lists()
lists.inIt()
addTaskBtn.addEventListener('click', createTask.bind(null, userInputAccess));
- This is what I was trying to implement. Bear in mind that I'm fetching the input from the user to create the task and this function is called through an event listener.
import { allTasks } from './App/Tasks.js';
function createTask(userInputAccess) {
if(userInputAccess.value.trim() === '') {
alert('You need to add a task!');
return;
};
import('./App/Tasks.js').then(module => {
const newTask = module.tasks(userInputAccess.value, 'active-list');
userInputAccess.value = '';
allTasks.push(newTask);
document.getElementById(newTask.set).append(newTask.html);
toggleListVisibility(allTasks);
});
}
addTaskBtn.addEventListener('click', createTask.bind(null, userInputAccess));
Is there any reason why this might not work? I thought that if I set it this way then the task module will only trigger once the user clicks on the addTaskBtn.
Thank you!
const newTask = new Task(...), but in dynamic code in the comment:const newTask = module.Task(...)withoutnew. It should beconst newTask = new module.Task(...).