Функции, которые я создал, работают хорошо, пока фрагмент, который я извлекаю, представляет собой один элемент, но когда я пытаюсь работать с более сложным компонентом, вложенные элементы не добавляются в DOM. Итак, вот код:
Код: Выделить всё
export class HtmlHandler {
static async fetchHtmlText(filename, directory){
const response = await fetch(`./components/${directory}/${filename}.html`);
if (!response.ok) throw new Error(`Failed to load ${filename}`);
return await response.text();
}
static async appendHtml(root, htmlText) {
root.insertAdjacentHTML("beforeend", htmlText);
return root.lastElementChild;
}
}
export class PageBuilder {
static async appendComponent(root, filename, directory) {
const html = await HtmlHandler.fetchHtmlText(filename, directory);
console.log(html);
return HtmlHandler.appendHtml(root, html);
}
Код: Выделить всё
Код: Выделить всё
async function fillGrid(grid) {
const unitArray = await objFromJson("manifest");
unitArray.forEach(async (element, index) => {
const unitSelector = await PageBuilder.appendComponent(grid, "UnitSelector", Dirs.COMPLEX);
console.log(unitSelector.outerHTML);
unitSelector.id = `unitSelector-${index}`;
unitSelector.textContent = element.title;
});
}
Я уже пробовал пару вещей, чтобы заставить его работать здесь, например, с тегом шаблона:
Код: Выделить всё
const template = document.createElement('template');
template.innerHTML = htmlText.trim();
const clone = template.content.cloneNode(true);
root.appendChild(clone);
return root.lastElementChild;
Подробнее здесь: https://stackoverflow.com/questions/798 ... -the-dom-c
Мобильная версия