如何在lit-html中使用chart.js

6mzjoqzu  于 2024-01-07  发布在  Chart.js
关注(0)|答案(1)|浏览(329)

我使用render()来创建和渲染lit-html

  1. import { html, render } from 'lit-html';
  2. const templateItems = [];
  3. const getChartInfo = (locationToRender) => {
  4. templateItems.push(html`
  5. <span>Welcome to charts </span>
  6. `);
  7. render(templateItems, locationToRender);
  8. };
  9. export default getChartInfo;

字符串
如何使用chart.js(https://www.chartjs.org/docs/latest/getting-started/usage.html)?
看一下文档,firstupdated()(https://lit.dev/docs/components/lifecle/#firstupdated)可能是正确的钩子?如果是这样,我该如何使用它?
如果没有,我该怎么做?

u91tlkcl

u91tlkcl1#

当使用lit-element时,你需要渲染一个画布,就像任何其他元素一样。

  1. render(): TemplateResult {
  2. return html`
  3. <div class="chart-wrapper">
  4. <canvas class="chart" ${ref(this.chartjsElementRef)}></canvas>
  5. </div>
  6. `;
  7. }

字符串
在生命周期期间,例如firstUpdated:

  1. protected firstUpdated(_changedProperties: PropertyValues) {
  2. super.firstUpdated(_changedProperties);
  3. this.chart = new Chart(
  4. this.chartjsElementRef.value!,
  5. <chartconfig>
  6. );
  7. }

展开查看全部

相关问题