6

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"/>

1 Answer 1

5

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';
Sign up to request clarification or add additional context in comments.

1 Comment

How about if the CoolApps namespacing is split across multiple files, e.g. CoolApps.ImageProcessing or CoolApps.PhotoUpload? Just not sure if namespacing into modules is even worth preserving?

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.