Laravel Mix I have no access to my javascript class

Viewed 261

In my laravel project i have written an custom form validation class. after compiling with npm run dev i dont have access to this class.

My files

small-form-validation.js

class SmallFormValidator {
    errMsgs = {
        required: 'This field is required!',
        string: 'Not valid string.',
        ...

my app.js

require('./bootstrap');
require('./myvendor/small-form-validator');

After compiling i found in the new created app.js file inside the public folder the source from class SmallFormValidator. It seems that he compiled right.

in my blade template i load with the mixing helper the app.js file. in my Javscript inside the blade template i cant create a instance of SmallFormValidation (var sfv = new SmallFormValidation() ).

I think the problems result that

  1. I dont work with module export and so on
  2. or i have some scope problems.

Does anyone know how to solve the problem?

1 Answers

an easy fix would be to assign it to your global/window object. in small-form-validation.js add window.SmallFormValidator = SmallFormValidator to make it globally accessible.

A better way would be to use module exports and imports

Related