1

I am trying to convert our existing VF components into LWC. For that I have to import some external JS. PFB code snippet. Not sure how to achieve this. Please help. enter image description here

1 Answer 1

5

You should download those files and save them in a zip file and upload in Static resources.

Then you should use import { loadStyle, loadScript } to load the resources in LWC component. PFB example:

import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import D3 from '@salesforce/resourceUrl/d3';
import DATA from './data';

export default class LibsD3 extends LightningElement {
    svgWidth = 400;
    svgHeight = 400;

    d3Initialized = false;

    renderedCallback() {
        if (this.d3Initialized) {
            return;
        }
        this.d3Initialized = true;

        Promise.all([
            loadScript(this, D3 + '/d3.v5.min.js'),
            loadStyle(this, D3 + '/style.css'),
        ])
            .then(() => {
                this.initializeD3();
            })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error loading D3',
                        message: error.message,
                        variant: 'error',
                    }),
                );
            });
    }
4
  • Thanks a lot. Is there a way to do it without uploading in Static Resources?
    – Swarnava
    Sep 20, 2019 at 6:59
  • Why some import statements are having curly braces and some are not? Jun 17, 2020 at 2:30
  • @AnuragBhardwaj you can check this Jun 17, 2020 at 3:07
  • Thanks salesforce-sas. It's really helpful. Jun 17, 2020 at 3:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .