简体   繁体   English

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

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

I recently ran into a problem using Charts JS in Lightning web components.我最近在 Lightning Web 组件中使用 Charts JS 时遇到了问题。 I wanted to share a solution I found for those who run into problems我想分享我为遇到问题的人找到的解决方案

How to manually handle data changes in a child component when they are updated on the parent.在父组件上更新时如何手动处理子组件中的数据更改。 This will work with everything but I was trying to update the Chartsjs example they show.这将适用于所有内容,但我试图更新他们展示的 Chartsjs 示例。

This is the solution I came up with.这是我想出的解决方案。

The parent Controller Has the following nested function父控制器具有以下嵌套函数

@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;
    }

}

The Parent Component has c-debt-chart component andd its receiving data from the all-date-records:父组件有 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>

The Problem was that the examples on Salesforce dont show how to update the data in charts js.问题是 Salesforce 上的示例没有显示如何更新图表 js 中的数据。 This is the solution I found using Getter and Setters这是我使用 Getter 和 Setter 找到的解决方案

Child Component debt chart子组件债务图表

<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>

Debt Chart Controller债务图表控制器

Every Time the variable allDateRecords changes at the parent level.每次变量 allDateRecords 在父级别发生变化。 It will trigger the child to update using the getter setter methods.它将触发孩子使用 getter setter 方法进行更新。 This way on the setter method it will fire the seperateDateObject function which does some logic to update the chart.这样,在 setter 方法上,它将触发 seperateDateObject 函数,该函数执行一些逻辑来更新图表。

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

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

Above Solution works when there are just one api properties that your logic is dependent on当您的逻辑仅依赖一个 api 属性时,上述解决方案有效

If you have more than one api properties which could change simultaneously which also should collectively be used in decision-making, you might wanna use renderedCallback() to get the logic executed.如果您有多个可以同时更改的 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
    }

calling somelogin within every setter might also result in abrupt UI behaviour during the properties are being set.在每个 setter 中调用 somelogin 也可能导致在设置属性期间出现突然的 UI 行为。

Instead反而

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

if rendered callback is not triggered, force the rendering by using the api properties within the template with if:true, helps in implementing fail-safe code如果未触发渲染回调,则通过使用模板中的 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.

相关问题 将数据从子级传递到父级,再传递到另一个子级组件React Js - Pass data from child to parent and to another child component React Js 在Vue js中将数据从父组件传递给子组件 - Pass data from parent component to child of child in Vue js 如何在 React.JS 中将数据从子组件传递到父组件? - How to pass data from child to parent component in React.JS? Vue.js - 使用发射但没有按钮将数据从子组件传递到父组件 - Vue.js - Pass data from child component to parent using emit but without button Aura.js Lightning组件:从上级/上级组件触发嵌套/子组件的方法吗? - Aura.js Lightning Components: Firing a nested/child component's methods from the super/parent component? 如何在不使用道具的情况下将数据从父组件传递到子组件 - How to pass data from parent to child component without using props 无法将数据从父组件传递给子组件 - Cannot pass data from Parent to Child component 在 ReactJs 中将数据从父组件传递到子组件 - Pass Data from Parent To Child Component in ReactJs 如果子组件为动态组件,如何将数据从子组件传递给父组件 - How to pass data from Child to Parent, if Child component dynamic component React JS 将数据或子组件传递给父组件 - React JS pass the data or child component to parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM