1

问题几乎是不言自明的。

例子:

function generateTemplate(tag) {
  return html`
    <${tag}
      .some-prop=${1}
    >
      ...
    </${tag}>
  `;
}
4

1 回答 1

4

没有一种方法可以具体执行您在此处提到的内容,但有两种方法可以让您更加接近:

  • 条件渲染

const template = tag => { 
  if (tag === 'custom-component') {
    return html`<custom-component></custom-component>`;
  } else if (tag === 'other-component') {
    return html`...`;
  } else {
    return html`<some-default></some-default>`;
  }
};

import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';
const template = unsafeContent => {
  // bear in mind that this should only be done after sanitizing the content
  return html`${unsafeHTML(unsafeContent)}`;
};
template('<my-component>Some content</my-component>');

于 2019-12-10T05:40:41.850 回答