O
O
origami10242020-11-19 17:17:13
Vue.js
origami1024, 2020-11-19 17:17:13

How to make a dynamic library import inside a method in vue?

At the front, I want to load the PDF generation library only by pressing the "generate PDF" button - download the library and start the generation.

Google dynamic import of components, but not libraries.

Is it possible to do something like this inside a vue method:

methods: {
        generatePDF() {
            const html2pdfd = () => import ("html2pdf" /*webpackChunkName:"html2Pdf"*/)
            html2pdfd()
        },
        ...
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2020-11-19
@origami1024

Maybe. You yourself wrote how. Further, the usual asynchrony has nothing to do with Vue or collectors.

spoiler
methods: {
        async generatePDF() {
            const html2pdfd = await import("html2pdf" /*webpackChunkName:"html2Pdf"*/);
            html2pdfd()
        },
        ...
}

or
methods: {
        generatePDF() {
            import("html2pdf" /*webpackChunkName:"html2Pdf"*/).than(html2pdfd => {
                html2pdfd()
            })
        },
        ...
}

V
Vladimir Korotenko, 2020-11-19
@firedragon

The person went to the page, so he needs it. And the component can beat on pages. What is the problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question