首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在html呈现中插入一个节点,其中包含发光元素web组件。

在html呈现中插入一个节点,其中包含发光元素web组件。
EN

Stack Overflow用户
提问于 2020-07-16 09:06:52
回答 2查看 1.1K关注 0票数 1

作为练习,给出html标记和一些重复(n),我想插入n倍标记节点使用点燃元素web组件。我被堆放在这里:

代码语言:javascript
复制
import {LitElement, html, css} from 'lit-element';

export class WebComponent extends LitElement {

    static get properties() {
        return {
            node: {type: String},
            repetitions: {type: Number}
        }
    };

    constructor() {
        super();
        this.node = "";
        this.repetitions = 0;
    }

    render() {
        var node = document.createElement(this.node);
        return html`
            <div>
                ${for (i = 0; i < this.repetitions; i++) { 
                    // Insert node here
                }}
            </div>
        `;
    };
}

customElements.define('web-component', WebComponent);

这个是可能的吗?多么?

EN

回答 2

Stack Overflow用户

发布于 2020-07-21 03:53:01

是的,就是这样!

您要寻找的可能是“未安全的html”,可以在包‘lit/directives/不安全-html’中获得;

来自lit html网站:

代码语言:javascript
复制
import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';

const markup = '<div>Some HTML to render.</div>';
const template = html`
    Look out, potentially unsafe HTML ahead:
    ${unsafeHTML(markup)}
`;

在您的示例中,可以这样使用:

代码语言:javascript
复制
render() {
    return html`
        <div>
            ${(() => {
                let htmlString = '';
                for (i = 0; i < this.repetitions; i++) { 
                    htmlString += `<${this.node}>Custom Node!</${this.node}>`;
                }
                return html`${unsafeHTML(htmlString)}`;
            })()}
        </div>
    `;
};

当函数名读取时,在使用它时要小心!

也来自lit html站点:

注意,这是不安全的使用任何用户提供的输入,但没有经过消毒或转义,因为它可能导致跨站点脚本漏洞。

测试了它,它与最新的版本一起工作!

不能告诉你的版本,但这似乎已经有一段时间了,所以你应该是好的!

票数 3
EN

Stack Overflow用户

发布于 2022-06-13 19:19:54

https://lit.dev/docs/templates/directives/#unsafehtml

代码语言:javascript
复制
import {unsafeHTML} from 'lit/directives/unsafe-html.js';

  [...]
  this.myhtml = "one<br />two"
  [...]

  render() {
    return html`
          <div>
            ${unsafeHTML(this.myhtml)}
          </div>
    `
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62931237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档