0

I am currently getting into typescript and webpack, but right now I am lost on how to solve this problem:

I want to call a function inside my typescript class when a form is being submitted. There is no magic involved just yet, but I just can't figure out how to make the functionality accessible to my form, I am constantly facing the problem that the function is not defined, so something must be missing to make it available to the window namespace.

This is my .ts file:

class Login {

    loginAccount() {
        console.log("test");
        debugger;
    }
};

export const login = new Login();

as you can see, no magic in here. I'm just trying to get the call done as of now.

This file is imported in my webpack like this (inside my index.ts):

import './Login/login.ts';

The form in my html file looks like this:

<form onsubmit="login.LoginAccount(); return false;">
    Email: <input type="text" name="email"><br>
    Password: <input type="text" name="password"><br>
    <input type="submit" name="Submit" value="Submit"/>
</form>

I just can't get this to work. I am sure that the solution is fairly simple, but after 30 minutes of googling this I mostly found solutions which deal with this in a jackhammer way.

2
  • <form onsubmit="login.loginAccount(); return false;"> Commented Apr 28, 2018 at 13:05
  • Right, I mistyped that one. Does not help, though, as login itself is not available Commented Apr 28, 2018 at 13:07

1 Answer 1

1

Webpack doesn't make all exports available from the window namespace. You can do it explicitly in your code.

class Login {
  loginAccount() {
    console.log("test");
    debugger;
  }
};
const login = new Login();
// as it's typescript you'd probably need (window as any).login = login 
window.login = login 
export { login };
Sign up to request clarification or add additional context in comments.

4 Comments

Sadly that does not help :( I added the code you mentioned in my login.ts, but I still can't access the object
Is the bundle included in the html?
Yes, it is. Other functionality I included in my index.ts already works flawlessly
I changed (window as any) to (<any>window) and it works now!

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.