Javascript 在带有lit元素web组件的html呈现中插入节点
作为练习,给定一个html标记和多次重复(n),我想使用lit元素web组件插入n次标记节点。我被堆积在这里:Javascript 在带有lit元素web组件的html呈现中插入节点,javascript,lit-element,lit-html,Javascript,Lit Element,Lit Html,作为练习,给定一个html标记和多次重复(n),我想使用lit元素web组件插入n次标记节点。我被堆积在这里: import {LitElement, html, css} from 'lit-element'; export class WebComponent extends LitElement { static get properties() { return { node: {type: String}, r
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);
从'lit element'导入{LitElement,html,css};
导出类WebComponent扩展了LitElement{
静态获取属性(){
返回{
节点:{type:String},
重复:{type:Number}
}
};
构造函数(){
超级();
this.node=“”;
这是重复次数=0;
}
render(){
var节点=document.createElement(this.node);
返回html`
${for(i=0;i
这可能吗?怎么做?是的,是的
您要查找的可能是“unsafeHTML”,可在“lit html/directions/unsafeHTML”包中找到
从lit html站点:
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)}
`;
从'lit html/directions/unsafe html.js'导入{unsafeHTML};
const markup='一些要呈现的HTML';
const template=html`
注意,前方可能存在不安全的HTML:
${unsafeHTML(标记)}
`;
在您的示例中,可以这样使用:
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>
`;
};
render(){
返回html`
${(() => {
让htmlString='';
对于(i=0;i
函数名显示时,使用时要小心
也可以从lit html站点:
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)}
`;
注意,这对于任何用户提供的输入都是不安全的
已清除或转义,因为这可能导致跨站点脚本漏洞
测试它,它与最新版本的作品无法说出你的版本,但这似乎已经存在了一段时间,所以你应该很好 是的,是的 您要查找的可能是“unsafeHTML”,可在“lit html/directions/unsafeHTML”包中找到 从lit html站点:
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)}
`;
从'lit html/directions/unsafe html.js'导入{unsafeHTML};
const markup='一些要呈现的HTML';
const template=html`
注意,前方可能存在不安全的HTML:
${unsafeHTML(标记)}
`;
在您的示例中,可以这样使用:
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>
`;
};
render(){
返回html`
${(() => {
让htmlString='';
对于(i=0;i
函数名显示时,使用时要小心
也可以从lit html站点:
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)}
`;
注意,这对于任何用户提供的输入都是不安全的
已清除或转义,因为这可能导致跨站点脚本漏洞
测试它,它与最新版本的作品无法说出你的版本,但这似乎已经存在了一段时间,所以你应该很好