0

I have been trying to convert an HTML template (includes JS and CSS) to VueJS app. The template is structured and modularised in such a way that JavaScript functions from other local JavaScript files are used repeatedly within other local files.

The problem now is that I am getting undefined variable errors in my JavaScript files when imported within a Vue single-page app.

For example:

file validate.js has validate_username() and validate_email()

file login.js has login()

file register.js has register() but uses validate_username() and login() from other files.

How I tried to import the files in Vue:


import './assets/validate.js';
import './assets/login.js';
import './assets/register.js';

I am getting validate_username() not defined in register.js

2
  • are using a bundler like vue cli or webpack? please share also one file content Commented Dec 26, 2020 at 19:29
  • I am using vue-cli. Commented Dec 26, 2020 at 19:30

1 Answer 1

1

You have to export the functions from their files:

// login.js
export const login = function() {
  ...
}

So that you can import them:

import { login } from './assets/login.js';

Alternatively, you could create a default export rather than a named export:

export default function() {  // login
  ...
}
import login from './assets/login.js';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.