Çözüldü DOM'a etki eden component yapımı

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

533388

Hectopat
Katılım
27 Mart 2022
Mesajlar
4.446
Makaleler
2
Çözümler
54
Mesela bir counter App örneği atabilir misiniz ya da mantığını anlatır mısınız? Ama component yapısında olacak şekilde.
@Kxaan hocam siz yardımcı olmuştunuz, müsait olunca bir daha bakabilir misiniz acaba?
 
Çözüm
Bu şekilde yaptım.
JavaScript:
class Counter extends HTMLElement {
  connectedCallback() {
    this.attachShadow({ mode: "open" });
    const text = this.shadowRoot.appendChild(document.createElement("p"));
    const button = this.shadowRoot.appendChild(document.createElement("button"));
    text.textContent = 0;
    button.textContent = "add";
    button.addEventListener("click", () => {
      text.textContent = Number(text.textContent) + 1;
    });
  }
}
customElements.define("number-counter", Counter);
HTML:
<number-counter></number-counter>

Burayı okumanı tavsiye ederim.
Bu şekilde yaptım.
JavaScript:
class Counter extends HTMLElement {
  connectedCallback() {
    this.attachShadow({ mode: "open" });
    const text = this.shadowRoot.appendChild(document.createElement("p"));
    const button = this.shadowRoot.appendChild(document.createElement("button"));
    text.textContent = 0;
    button.textContent = "add";
    button.addEventListener("click", () => {
      text.textContent = Number(text.textContent) + 1;
    });
  }
}
customElements.define("number-counter", Counter);
HTML:
<number-counter></number-counter>

Burayı okumanı tavsiye ederim.
 
Çözüm
Bu şekilde yaptım.
JavaScript:
class Counter extends HTMLElement {
 connectedCallback() {
 this.attachShadow({ mode: "open" });
 const text = this.shadowRoot.appendChild(document.createElement("p"));
 const button = this.shadowRoot.appendChild(document.createElement("button"));
 text.textContent = 0;
 button.textContent = "add";
 button.addEventListener("click", () => {
 text.textContent = Number(text.textContent) + 1;
 });
 }
}
customElements.define("number-counter", Counter);
HTML:
<number-counter></number-counter>

Heh hocam ben siz yazmadan önce birkaç video izledim de birkaç soru oluştu kafamda.
İnternette Shadow dom ile ilgi araştırma yaptım ve body içinde oluşturduğumuz elementlerin bir Shadow host ve hostun içinde de elementler olduğunu ve bunlarım Shadow root içinde yer aldığını anladım, doğru mudur? Değilse nedir?

This. Attachshadow({ mode: "open" });

Bu kodun amacı oluşturduğumuz elementi shadowroot ağacı altına eklemek için istek göndermek mi? Eğer closed olursa null değer mi dönüyor?
 
Son düzenleme:

Yeni konular

Geri
Yukarı