src/my-counter.ts
:
import { LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { styleMap } from "lit/directives/style-map.js";
@customElement("my-counter")
export class MyCounter extends LitElement {
@property({ type: Number })
count = 0;
render() {
const styles = { color: "" };
if (this.count >= 0) {
styles.color = "green";
} else {
styles.color = "red";
}
return html`
<div>
<button @click=${this._onDec} part="button"> - </button>
count is: <span style=${styleMap(styles)}>${this.count}</span>
<button @click=${this._onInc} part="button"> + </button>
</div>
`;
}
private _onDec() {
this.count--;
}
private _onInc() {
this.count++;
}
static styles = css`
button {
border-radius: 4px;
border: 1px solid transparent;
background-color: limegreen;
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"my-counter": MyCounter;
}
}
index.html
:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Lit + TS</title>
<script type="module" src="/src/my-counter.ts"></script>
</head>
<body>
<my-counter count="5"></my-counter>
<my-counter count="-5"></my-counter>
</body>
</html>
标签:styles,count,修改,样式,lit,color,html,import
From: https://www.cnblogs.com/soarowl/p/18365264