自定义聚合物组件未触发sweetalert。

huangapple go评论70阅读模式
英文:

Custom polymer component not triggering sweetalert

问题

I have created a custom card component in polymer:

import { PolymerElement, html } from 'https://cdnjs.cloudflare.com/ajax/libs/polymer/3.5.1/polymer-element.js';

class CustomCard extends PolymerElement {
  static get template() {
    return html`
      <style>
        .card {
          border: 1px solid black;
          border-radius: 4px;
          box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
          background-color: #fff;
        }
        .card-header {
          display: flex;
          align-items: center;
          justify-content: space-between;
          background-color: indigo;
          padding: 10px;
          color: white;
        }
        .card-title {
          text-align: [[_getTitleAlignment(hasButtons)]];
        }
        .card-buttons {
          display: flex;
          align-items: center;
          gap: 8px;
        }
        .card-body {
          padding: 10px;
        }
      </style>

      <div class="card">
        <div class="card-header">
          <div class="card-title">[[title]]</div>
          <div class="card-buttons" hidden$="[[!hasButtons]]">
            <slot name="buttons"></slot>
          </div>
        </div>
        <div class="card-body">
          <slot name="body"></slot>
        </div>
      </div>

      <!-- SweetAlert library -->
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>

      <script>
        document.addEventListener('DOMContentLoaded', () => {
          const btn1 = document.getElementById('btn1');
          const btn2 = document.getElementById('btn2');

          btn1.addEventListener('click', () => {
            Swal.fire('Save Button Clicked');
          });

          btn2.addEventListener('click', () => {
            Swal.fire('Delete Button Clicked');
          });
        });
      </script>
    `;
  }

  static get properties() {
    return {
      title: {
        type: String,
        value: '',
      },
      hasButtons: {
        type: Boolean,
        value: false,
      },
    };
  }

  _getTitleAlignment(hasButtons) {
    return hasButtons ? 'left' : 'center';
  }
}

customElements.define('custom-card', CustomCard);

And I am using this way:

<custom-card title="Card Title" has-buttons>
    <div slot="buttons">
      <button id="btn1" class="btn btn-primary">
        <i class="fas fa-save"></i> Save
      </button>
      <button id="btn2" class="btn btn-danger">
        <i class="fas fa-trash"></i> Delete
      </button>
    </div>
    <div slot="body">
      <p>This is the content of the card.</p>
    </div>
</custom-card>

However, on click, no SweetAlert is fired. How can I fix it?

英文:

I have created a custom card component in polymer

import { PolymerElement, html } from &#39;https://cdnjs.cloudflare.com/ajax/libs/polymer/3.5.1/polymer-element.js&#39;;
class CustomCard extends PolymerElement {
static get template() {
return html`
&lt;style&gt;
.card {
border: 1px solid black;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background-color: #fff;
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
background-color: indigo;
padding: 10px;
color: white;
}
.card-title {
text-align: [[_getTitleAlignment(hasButtons)]];
}
.card-buttons {
display: flex;
align-items: center;
gap: 8px;
}
.card-body {
padding: 10px;
}
&lt;/style&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;div class=&quot;card-header&quot;&gt;
&lt;div class=&quot;card-title&quot;&gt;[[title]]&lt;/div&gt;
&lt;div class=&quot;card-buttons&quot; hidden$=&quot;[[!hasButtons]]&quot;&gt;
&lt;slot name=&quot;buttons&quot;&gt;&lt;/slot&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;slot name=&quot;body&quot;&gt;&lt;/slot&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- SweetAlert library --&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/sweetalert2@10&quot;&gt;&lt;/script&gt;
&lt;script&gt;
document.addEventListener(&#39;DOMContentLoaded&#39;, () =&gt; {
const btn1 = document.getElementById(&#39;btn1&#39;);
const btn2 = document.getElementById(&#39;btn2&#39;);
btn1.addEventListener(&#39;click&#39;, () =&gt; {
Swal.fire(&#39;Save Button Clicked&#39;);
});
btn2.addEventListener(&#39;click&#39;, () =&gt; {
Swal.fire(&#39;Delete Button Clicked&#39;);
});
});
&lt;/script&gt;
`;
}
static get properties() {
return {
title: {
type: String,
value: &#39;&#39;,
},
hasButtons: {
type: Boolean,
value: false,
},
};
}
_getTitleAlignment(hasButtons) {
return hasButtons ? &#39;left&#39; : &#39;center&#39;;
}
}
customElements.define(&#39;custom-card&#39;, CustomCard);

and i am using this way

&lt;custom-card title=&quot;Card Title&quot; has-buttons&gt;
&lt;div slot=&quot;buttons&quot;&gt;
&lt;button id=&quot;btn1&quot; class=&quot;btn btn-primary&quot;&gt;
&lt;i class=&quot;fas fa-save&quot;&gt;&lt;/i&gt; Save
&lt;/button&gt;
&lt;button id=&quot;btn2&quot; class=&quot;btn btn-danger&quot;&gt;
&lt;i class=&quot;fas fa-trash&quot;&gt;&lt;/i&gt; Delete
&lt;/button&gt;
&lt;/div&gt;
&lt;div slot=&quot;body&quot;&gt;
&lt;p&gt;This is the content of the card.&lt;/p&gt;
&lt;/div&gt;
&lt;/custom-card&gt;

However on click no sweetalert is fired.

How can i fix it?

答案1

得分: 1

以下是您提供的内容的翻译:

为什么要使用几乎不使用的6KB Lit库。
在同样的时间里,您可以学习原生JavaScript Web组件,它:

  • 没有依赖项,
  • 不需要构建步骤,
  • 体积更小,
  • 更快,
  • 更易维护,因为您不必担心未来框架更新引发的破坏性变更。

用斧头砍一块木头,用链锯砍倒整棵树

customElements.define('notification-item', class extends HTMLElement {
  static get observedAttributes() {
    return ["title", "subtitle", "imageurl", "relativetime", "dotcolor"];
  };
  constructor() {
    super().attachShadow({mode: "open"}).innerHTML = this.html();
    this.shadowRoot.querySelector('[type="checkbox"]')
                   .onclick = (evt) => this.checkboxClick(evt);
  }
  checkboxClick(evt) {
    // 在此处打开SweetAlert Swal.fire('Checkbox Clicked!');
    alert("checkboxClick")
  }
  attributeChangedCallback(name, oldValue, newValue) {
    let el = this.shadowRoot.getElementById(name);
    if (el) {
      if (name == "imageurl") el.src = newValue;
      else if (name == "dotcolor") el.style.color = newValue;
      else el.innerHTML = newValue;
    } else {
      console.warn("No:", name);
    }
  }
  html() {
    return `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
    <style>
      .notification-item {border:1px solid black;border-radius:10px;padding:20px;margin-bottom:20px;transition: opacity 0.3s ease}
      .notification-item:hover {opacity:0.8}
      .custom-checkbox {width: 14px;height:14px}
     </style>
    <div class="notification-item">
      <div class="row align-items-center">
        <div class="col-1">
          <input type="checkbox" class="custom-checkbox"/>
          <i class="fas fa-circle" id="dotcolor"></i>
        </div>
        <div class="col-2">
          <img id="imageurl"
            alt="Profile"
            width="60"
            height="60"
            class="img-fluid rounded-circle"
          />
        </div>
        <div class="col-6">
          <div class="fw-bold" id="title"></div>
          <div id="subtitle"></div>
        </div>
        <div class="col-2">
          <div id="relativetime"></div>
        </div>
        <div class="col-1">
          <i class="fas fa-bookmark"></i>
        </div>
      </div>
    </div>`
  }
});
<notification-item 
  title="您查看的图片已更新"
  subtitle="已更新的图片"
  imageurl="https://images.freeimages.com/fic/images/icons/747/network/256/user_group.png"
  relativetime="2小时前发布"
  dotcolor="green"></notification-item>

<!-- Font Awesome图标需要在主文档和Web组件中加载 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />

希望这个翻译对您有帮助。

英文:

Why a 6KB Lit library, which you hardly use.
In the same time you learned Lit you can learn native JavaScript Web Components

  • which has no dependencies,
  • requires no build step
  • is smaller,
  • is faster
  • and is more maintainable because you won't have to worry about any future Framework updates causing breaking changes.

> Use an axe to chop one block, use a chainsaw to chop down the tree

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

customElements.define(&#39;notification-item&#39;, class extends HTMLElement {
static get observedAttributes() {
return [&quot;title&quot;, &quot;subtitle&quot;, &quot;imageurl&quot;, &quot;relativetime&quot;, &quot;dotcolor&quot;];
};
constructor() {
super().attachShadow({mode:&quot;open&quot;}).innerHTML = this.html();
this.shadowRoot.querySelector(&#39;[type=&quot;checkbox&quot;]&#39;)
.onclick = (evt) =&gt; this.checkboxClick(evt);
}
checkboxClick(evt) {
// Open SweetAlert here Swal.fire(&#39;Checkbox Clicked!&#39;);
alert(&quot;checkboxClick&quot;)
}
attributeChangedCallback(name, oldValue, newValue) {
let el = this.shadowRoot.getElementById(name);
if (el) {
if (name == &quot;imageurl&quot;) el.src = newValue;
else if (name == &quot;dotcolor&quot;) el.style.color = newValue;
else el.innerHTML = newValue;
} else {
console.warn(&quot;No:&quot;, name);
}
}
html() {
return `&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot; /&gt;
&lt;style&gt;
.notification-item {border:1px solid black;border-radius:10px;padding:20px;margin-bottom:20px;transition: opacity 0.3s ease}
.notification-item:hover {opacity:0.8}
.custom-checkbox {width: 14px;height:14px}
&lt;/style&gt;
&lt;div class=&quot;notification-item&quot;&gt;
&lt;div class=&quot;row align-items-center&quot;&gt;
&lt;div class=&quot;col-1&quot;&gt;
&lt;input type=&quot;checkbox&quot; class=&quot;custom-checkbox&quot;/&gt;
&lt;i class=&quot;fas fa-circle&quot; id=&quot;dotcolor&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;img id=&quot;imageurl&quot;
alt=&quot;Profile&quot;
width=&quot;60&quot;
height=&quot;60&quot;
class=&quot;img-fluid rounded-circle&quot;
/&gt;
&lt;/div&gt;
&lt;div class=&quot;col-6&quot;&gt;
&lt;div class=&quot;fw-bold&quot; id=&quot;title&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;subtitle&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;div id=&quot;relativetime&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-1&quot;&gt;
&lt;i class=&quot;fas fa-bookmark&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;`}
});

<!-- language: lang-html -->

&lt;notification-item 
title=&quot;An image you viewed has been updatedsss&quot; 
subtitle=&quot;Imagedddd updated&quot;
imageurl=&quot;https://images.freeimages.com/fic/images/icons/747/network/256/user_group.png&quot;
relativetime=&quot;Posted 2d hours ago&quot; dotcolor=&quot;green&quot;&gt;&lt;/notification-item&gt;
&lt;!-- FA icons require loading in the main document AND the Web Component --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot; /&gt;

<!-- end snippet -->

答案2

得分: 0

I switched to lit element and my component now works

import { LitElement, html, css } from 'https://unpkg.com/lit-element?module';

class NotificationItem extends LitElement {
  static styles = css`
    .notification-item {
      border: 1px solid black;
      border-radius: 10px;
      padding: 20px;
      margin-bottom: 20px;
      transition: opacity 0.3s ease;
    }

    .notification-item:hover {
      opacity: 0.8;
    }

    .custom-checkbox {
      width: 14px;
      height: 14px;
    }
  `;

  static properties = {
    title: { type: String },
    subtitle: { type: String },
    imageurl: { type: String },
    relativetime: { type: String },
    dotcolor: { type: String },
  };

  handleCheckboxClick() {
    // Open SweetAlert here
    Swal.fire('Checkbox Clicked!');
  }

  render() {
    return html`
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" />
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />

      <div class="notification-item">
        <div class="row align-items-center">
          <div class="col-1">
            <input
              type="checkbox"
              class="custom-checkbox"
              @click=${this.handleCheckboxClick}
            />
            <i class="fas fa-circle" style="color: ${this.dotcolor};"></i>
          </div>
          <div class="col-2">
            <img
              src="${this.imageurl}"
              alt="Profile"
              width="60"
              height="60"
              class="img-fluid rounded-circle"
            />
          </div>
          <div class="col-6">
            <div class="fw-bold">${this.title}</div>
            <div>${this.subtitle}</div>
          </div>
          <div class="col-2">
            <div>${this.relativetime}</div>
          </div>
          <div class="col-1">
            <i class="fas fa-bookmark"></i>
          </div>
        </div>
      </div>
    `;
  }
}

customElements.define('notification-item', NotificationItem);

and used it like this

<notification-item
  title="An image you viewed has been updatedsss"
  subtitle="Imagedddd updated"
  imageurl="https://images.freeimages.com/fic/images/icons/747/network/256/user_group.png"
  relativetime="Posted 2d hours ago"
  dotcolor="green"
></notification-item>
英文:

I switched to lit element and my component now works

import { LitElement, html, css } from &#39;https://unpkg.com/lit-element?module&#39;;
class NotificationItem extends LitElement {
static styles = css`
.notification-item {
border: 1px solid black;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
transition: opacity 0.3s ease;
}
.notification-item:hover {
opacity: 0.8;
}
.custom-checkbox {
width: 14px;
height: 14px;
}
`;
static properties = {
title: { type: String },
subtitle: { type: String },
imageurl: { type: String },
relativetime: { type: String },
dotcolor: { type: String },
};
handleCheckboxClick() {
// Open SweetAlert here
Swal.fire(&#39;Checkbox Clicked!&#39;);
}
render() {
return html`
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot; /&gt;
&lt;div class=&quot;notification-item&quot;&gt;
&lt;div class=&quot;row align-items-center&quot;&gt;
&lt;div class=&quot;col-1&quot;&gt;
&lt;input
type=&quot;checkbox&quot;
class=&quot;custom-checkbox&quot;
@click=${this.handleCheckboxClick}
/&gt;
&lt;i class=&quot;fas fa-circle&quot; style=&quot;color: ${this.dotcolor};&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;img
src=&quot;${this.imageurl}&quot;
alt=&quot;Profile&quot;
width=&quot;60&quot;
height=&quot;60&quot;
class=&quot;img-fluid rounded-circle&quot;
/&gt;
&lt;/div&gt;
&lt;div class=&quot;col-6&quot;&gt;
&lt;div class=&quot;fw-bold&quot;&gt;${this.title}&lt;/div&gt;
&lt;div&gt;${this.subtitle}&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;div&gt;${this.relativetime}&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-1&quot;&gt;
&lt;i class=&quot;fas fa-bookmark&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
`;
}
}
customElements.define(&#39;notification-item&#39;, NotificationItem);

and used it like this

    &lt;notification-item
title=&quot;An image you viewed has been updatedsss&quot;
subtitle=&quot;Imagedddd updated&quot;
imageurl=&quot;https://images.freeimages.com/fic/images/icons/747/network/256/user_group.png&quot;
relativetime=&quot;Posted 2d hours ago&quot;
dotcolor=&quot;green&quot;
&gt;&lt;/notification-item&gt;

huangapple
  • 本文由 发表于 2023年6月9日 01:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定