Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access variables after webpack compilation

I can't access variables after webpack compilation. I need it, because i'm using this variable to call function in onclick.

code is like

function DetailClass() {
  this.add = function() {
    console.log(1);
    }
}

var Detail = new DetailClass();

Webpack including this code into eval()

And in HTML i call it like

<div onclick="Detail.add();">Add</div>
like image 489
Detryer Avatar asked Feb 05 '23 06:02

Detryer


1 Answers

In your webpack configuration you have to specify output.library and output.libraryTarget.

Here's a guide: https://webpack.github.io/docs/configuration.html#output-library

For following configuration:

...
output:{
  ...
  library: 'MyLib',
  libraryTarget: 'var'
}
...

You will be able to use it like:

<div onclick="MyLib.Detail.add();">Add</div>
like image 56
DDRamone Avatar answered Feb 16 '23 15:02

DDRamone