Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
custom component argument not passed

I am currently familiarizing myself with the concept of web components.

I am trying to give the custom component a stylesheet, so that it can mimic the page content. But the argument I am trying to pass seems to be 'null' from the view of DiceComponent.js.

What I would expect to happen is:

  1. The component-style argument is succesfully received
  2. As a result, the webcomponent loads the intended stylesheet.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Web Component Test</title>
    <script src="DiceComponent.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<p>if it works, you shall see a webcomponent below. </p>
<p>if the webcomponent manages to use the same style as the page, it should turn green!</p>
<my-diceroll component-style="style.css"></my-diceroll>

</body>
</html>

DiceComponent.js

class DiceRoll extends HTMLElement {

    constructor() {
        super();
        const shadow = this.attachShadow({mode:'open'});
        let paragraph = document.createElement("p");
        paragraph.textContent = "GREEN when argument is passed, RED when not";

        //style stuff
        const style = document.createElement('link');
        style.setAttribute('rel','stylesheet')
        style.href = this.hasAttribute('component-style') ? this.getAttribute('component-style') : 'ComponentStyle.css';

        shadow.append(paragraph);
        shadow.append(style);
    }
}

window.customElements.define('my-diceroll', DiceRoll);

style.css

p {
    color: green;
}

ComponentStyle.css

p {
    color: red;
}

current result from (chrome) browser:

browser-result

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Apparently, the arguments are not yet available in the constructor. Only after the component is added to the DOM tree.

This issue is solved by migrating the code to the connectedCallback routine, which gets called after the component is added to the DOM tree.

    connectedCallback(){
        const shadow = this.shadowRoot;
        const style = document.createElement('link');
        style.id = "style";

        style.setAttribute('rel','stylesheet')
        style.href = this.hasAttribute('component-style') ? this.getAttribute('component-style') : 'ComponentStyle.css';

        shadow.append(style);
    }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda