繁体   English   中英

使用 Charts Js 在 Lightning Web 组件中将数据从父级传递给子级

[英]Pass data from parent to child in lightning web component using Charts Js

我最近在 Lightning Web 组件中使用 Charts JS 时遇到了问题。 我想分享我为遇到问题的人找到的解决方案

在父组件上更新时如何手动处理子组件中的数据更改。 这将适用于所有内容,但我试图更新他们展示的 Chartsjs 示例。

这是我想出的解决方案。

父控制器具有以下嵌套函数

@wire(getRecord, { recordId: '$recordId', fields: CONTACT_FIELDS })
wireContact({ error, data }) {
    if (data) {
        console.log('getRecord Data', JSON.stringify(data))
        this.contact = data;
        getAllDateRecords({ contactId: this.recordId })
            .then(result => {
                this.allDateRecords = result;
                this.chartReady = true;
            })
            .catch(err => console.log(JSON.stringify(err)));
    } else if (error) {
        console.error('error', error)
        this.contact = undefined;
    }

}

父组件有 c-debt-chart 组件和它从所有日期记录接收数据:

<template>
<div class="slds-page-header__row slds-accordion__content">
                <div class="c-container w-100">
                    <lightning-layout horizontal-align="space">
                        <lightning-layout-item padding="around-small">
                            <template if:true={chartReady}>
                                <c-debt-chart all-date-records={allDateRecords}></c-debt-chart>
                            </template>
                        </lightning-layout-item>
                    </lightning-layout>
                </div>
            </div>
<template>

问题是 Salesforce 上的示例没有显示如何更新图表 js 中的数据。 这是我使用 Getter 和 Setter 找到的解决方案

子组件债务图表

<template>
    <lightning-card title="Debt Overview" icon-name="standard:currency">
        <div class="slds-m-around_medium">
            <canvas class="donut" lwc:dom="manual"></canvas>
        </div>
    </lightning-card>
</template>

债务图表控制器

每次变量 allDateRecords 在父级别发生变化。 它将触发孩子使用 getter setter 方法进行更新。 这样,在 setter 方法上,它将触发 seperateDateObject 函数,该函数执行一些逻辑来更新图表。

@api
get allDateRecords() {
    return this._allDateRecords;
}

set allDateRecords(value) {
    this._allDateRecords = value;
    this.separateDateObject();
}

当您的逻辑仅依赖一个 api 属性时,上述解决方案有效

如果您有多个可以同时更改的 api 属性,这些属性也应该共同用于决策,您可能想要使用 renderCallback() 来执行逻辑。

    @api
    get prop1() {
        return this._prop1;
    }
    set prop1(value) {
        this._prop1= value;
        this.somelogic();    //needs to be called in every dependent set prop
    }
    
    
    @api
    get prop2() {
        return this._prop2;
    }
    set prop2(value) {
        this._prop2= value;
        this.somelogic();   //needs to be called in every dependent set prop
    }

    somelogic(){
        this.showsomething = this._prop1 && this._prop2; //dependent logic
    }

在每个 setter 中调用 somelogin 也可能导致在设置属性期间出现突然的 UI 行为。

反而

renderedCallback(){
    this.somelogic();    //can even execute code conditionally
}

如果未触发渲染回调,则通过使用模板中的 api 属性强制渲染 if:true,有助于实现故障安全代码

<template if:true={_prop1}>
    <template if:true={_prop2}>
         <!--place dependant html here-->
    <template>
<template>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM