I am trying to do something simple in laravel. I have a simple js module but I am unable to init it with laravel (laravel-mix to compile). To test I want to do something as simple as:
<button onclick="console.log(Test.sum);">Print</button>
Uncaught TypeError: Cannot read properties of undefined (reading 'sum')
test.js
var Test = function() {
var a = 0;
var b = 1;
var _sum = function() {
return a + b;
}
return {
sum: function() {
_sum();
}
};
}();
app.js
const Test = require('./test');
My question, what is the propper way to init this module to use in my html web?
thanks a lot!
Solution:
test.js
var test = function() {
var a = 0;
var b = 1;
var sum = function() {
return a + b;
}
return {
sum: function() {
return sum();
}
};
}();
export { test }
app.js
import { test } from './test';
window.test = test ;
Via Active questions tagged javascript - Stack Overflow https://ift.tt/O1SvydL
Comments
Post a Comment