Typescript Modules And Systemjs. Instantiate Class From Inline Script
I am transpiling a typescript module to javascript with the system module option. I am executing this in a browser. I am able to consume this module when the code to initialize the
Solution 1:
You can use SystemJS to import the module in Javascript.
Assuming you have a module named app.ts that exports a variable named value.
app.ts:
exportlet value = 'ABCASF';
In a script tag you can write:
System.import('app.js').then(function(appModule) {
console.log(appModule.value);
}, console.error.bind(console));
Keep in mind that the module name you have to give to System.import might vary according to your setup.
TypeScript classes get transpiled to ES5 functions so you can make use of them in javascript this way.
example.ts:
exportclassExample {
constructor(public someValue: string, private someOtherValue: number) {
}
public method() {
returnthis.someOtherValue;
}
}
And in the script tag this way:
System.import('example.js').then(function(example) {
// Example class is a function on the module // use new to make a new instancevar value = new example.Example('a', 5);
// work as usual console.log(value.method());
}, console.error.bind(console));
Post a Comment for "Typescript Modules And Systemjs. Instantiate Class From Inline Script"