Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Quiero buscar e insertar un atributo varias veces

Quiero activar el contenido de todos los atributos en un elemento personalizado <my-component> . Actualmente, solo se ejecuta el atributo anterior.

Si funciona, será <p>hello, world! welcom </p> .

Además, quiero que la parte {{ }} se ejecute con o sin espacios.

 function component(elementName, ComponentOptions) { customElements.define(`${elementName}`, class extends HTMLElement { connectedCallback() { if (ComponentOptions.return) { if (this.getAttributeNames()) { const AttrNames = this.getAttributeNames(); var optionsreturn = ComponentOptions.return; AttrNames.forEach(attr => { let val = this.getAttribute(attr); optionsreturn = optionsreturn.replace(`{{ ${attr} }}`, val); this.outerHTML = optionsreturn; }); } else { this.outerHTML = ComponentOptions.return } } } }); } component("my-component", { return: `<p>hello, {{ w }} {{ wel }} </p>` })
 <my-component w="world!" wel="welcom"></my-component>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe mover su this.outerHTML = optionsreturn; fuera del ciclo forEach .

También cambié la cadena a una expresión regular en su llamada .replace() . Ahora aceptará cero o un espacio alrededor de los nombres de sus atributos.

 function component(elementName, ComponentOptions) { customElements.define(elementName, class extends HTMLElement { connectedCallback() { if (ComponentOptions.return) { if (this.getAttributeNames()) { const AttrNames = this.getAttributeNames(); var optionsreturn = ComponentOptions.return; AttrNames.forEach(attr => { let val = this.getAttribute(attr); optionsreturn = optionsreturn.replace(new RegExp(`\{\{ ?${attr} ?\}\}`,"g"), val); }); this.outerHTML = optionsreturn; } else { this.outerHTML = ComponentOptions.return } } } }); } component("my-component", { return: `<p>hello, {{ w }} {{wel}} </p>` })
 <my-component w="world!" wel="welcom"></my-component>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!