gpt4 book ai didi

javascript - lit-element 可以跟踪其范围之外的变量吗

转载 作者:行者123 更新时间:2023-12-01 00:30:25 25 4
gpt4 key购买 nike

在我的 lit-element 中,我正在根据外部变量渲染一些内容。我如何知道该变量发生变化时进行更新?

import { LitElement, html } from 'lit-element';
import './element01.js';

class Layout extends LitElement {
createRenderRoot(){ return this; }

static get properties() {
return {
settings: { Object }
};
}

constructor() {
super();
}

render() {
return html`
${(settings.foo === 'bar')? html`<my-element01 />` : null}
`;
}
}
customElements.define('my-layout', Layout);

设置对象正在被外部修改,这个元素如何知道要更新?我没有使用任何其他框架。

最佳答案

index.html 文件应该始终有一个

<my-app></my-app>

在 my-app 元素中,您将能够使用 litElement 的所有功能,例如

 class MyApp extends LitElement {

static get properties() {
return {
prop: {type: Object},
};
}

render() {
return html`
<my-element .prop="${this.prop}"</my-element>
<my-server .prop="${this.prop}"</my-server>
<button @onClick='${(e) => this.prop += 1}' >change the settings value</button>
`;
}
}

关于javascript - lit-element 可以跟踪其范围之外的变量吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com