提问人:Gandalf 提问时间:6/9/2023 最后编辑:Gandalf 更新时间:6/9/2023 访问量:52
定制聚合物组件不会触发 sweetalert
Custom polymer component not triggering sweetalert
问:
我用聚合物创建了一个自定义卡片组件
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);
我正在使用这种方式
<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>
但是,在单击时,不会触发任何甜蜜警报。
我该如何解决?
答:
0赞
Gandalf
6/9/2023
#1
我切换到 lit 元素,我的组件现在可以工作了
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);
并像这样使用它
<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>
1赞
Danny '365CSI' Engelman
6/9/2023
#2
为什么是一个 6KB 的 Lit 库,你几乎不使用。 在学习 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) {
// Open SweetAlert here 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="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>
<!-- FA icons require loading in the main document AND the Web Component -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
评论
0赞
Gandalf
6/9/2023
使用像 Lit 这样的库可以抽象出一些复杂性,并为创建 Web 组件提供更高级别的声明性语法。我还认为,我们都已经厌倦了JavaScript工业园区的高大上的故事。
1赞
Danny '365CSI' Engelman
6/9/2023
他们是开汤者,无法控制味道。花 2 周时间,你已经构建了自己的 BaseClass(并从 Lit 及其 60+ 替代品中窃取) 所有这些 BaseClass 都是新的 jQuery
0赞
Gandalf
6/9/2023
当我有时间时,我会从 lit 转换为原生 JavaScript Web 组件。
评论