styles - litElement 在静态样式中使用类变量
问题描述
我需要在我的 litElement 类中计算相同的弹出框位置。接下来将其用作样式值。例如。:
export const defaultProps = {
wrapperTop: 0,
}
export class Popover extends ScopedElementsMixin(LitElement) {
...
static get properties() {
return {
wrapperTop: { type: String || Number, reflect: true},
}
}
...
constructor() {
super();
this.wrapperTop = this.defaultProps.wrapperTop
}
static get styles() {
return css`
.popover-wrapper {
top: ${this.wrapperTop} //this.wrapperTop is undefined
}`
}
calculateY () {
//calculate Y depend on some element position
}
update() {
calculateY()
}
...
return html`<div class="popoverWrapper"> ... </div>`
}
而且我找不到使用 this.wrapperTop 获取静态样式的方法。它是未定义的。有一些方法可以做到这一点,或者我必须在渲染中使用 style=""?
解决方案
添加unsafeCSS
到您的导入:
import { html, css, LitElement, unsafeCSS } from 'lit-element';
使用
static get styles() {
return css`
.popover-wrapper {
top: ${unsafeCSS(this.wrapperTop)} //this.wrapperTop is undefined
}`
}
推荐阅读
- swift - 我想在一周中的特定日子发送通知
- asp.net - 当用户具有角色请求另一个视图需要角色时重定向到登录
- javascript - 如何读出受表单保护的站点 - 使用 nodejs
- azure-data-factory - 如何在未检测到 Azure 数据工厂 v2 架构中为 json 设置复制活动
- shopify - 将产品系列分配给 Shopify 中自定义创建的产品系列页面?
- image - imtranslate 命令 matlab 显示黑色图像
- email - 如何为联系表格网页设置邮件标题
- typescript - knexjs、typescript 和 mariadb 的事务问题
- android - 底部导航中的 Android 切换
- python - python中的变量%d和+=运算符