Given a TypeScript class which is namespaced as a TS module, in a file CoolApps.Utilities.ts:
module CoolApps {
export class Utilities {
myMethod(){
alert("something awesome");
}
}
}
The class works in a normal TypeScript app but I'm trying to figure our the correct way to reference this class in Angular 2. How do I use this in an Angular 2 app (Ionic 2 in the case)? So far the following doesn't resolve so I'm probably getting the syntax wrong:
import {Page} from 'ionic-framework/ionic';
import {Utilities} from '../../core/CoolApps.Utilities';
Using a reference like so will allow the editor to see the code as valid, but Angular can't resolve it (maybe import only works for Angular specific modules?):
///<reference path="../../core/mapping/OCM.Mapping.ts"/>
Use a file tsconfig. @Pavel_K In the TypeScript handbook: "To reiterate why you shouldn't try to namespace your module contents, the general idea of namespacing is to provide logical grouping of constructs and to prevent name collisions.
Import :
import {CoolApps} from '../../core/CoolApps.Utilities';
Class usage example
let util : CoolApps.Utilities = new CoolApps.Utilities();
You can also remove module declaration from CoolApps.Utilities.ts and transform the import like this :
import * as CoolApps from '../../core/CoolApps.Utilities';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With