javascript - TypeScript - Attach module/namespaces to window -
i've built lot of apis , applications using es2015, not used best practices in typescript yet, maybe can me. let's building api/sdk shop. goal user includes js file , accesses shop , namespaces via window object, possible angular , other libs well.
window.shop.init(); window.shop.cart.get(); window.shop.cart.set(); window.shop.cart.clear();
in ecmascript 2015, write methods get
, set
, import them in main file , extend shop object , global object.
// in cart.js namespace file export {get} './get'; // in shop.js import * cart './cart'; global.shop = { cart }
being approach namespacing in es2015, feels kinda wrong in typescript having module , namespace keywords.
i want achieve same in ts. tried things following, no success.
module shop { export const cart = {...} } (<any>window).shop = shop;
or
namespace shop { // ... } (<any>window).shop = shop;
there tutorials claiming module automatically attached global/window object, did not happen me.
i using typescript 1.8.10. appreciated!
there tutorials claiming module automatically attached global/window object, did not happen me.
maybe because code of namespace
in (es6) module instead of in script? differences between scripts , modules detailed here.
the code below makes global variable shop
in browser if loaded script, ie tag <script src="shop.js">
(or can concatenate file other javascript files, example uglifyjs
).
// file shop.ts namespace shop { export const cart = { /* ... */ } }
if code loaded es6 module (ie of webpack, systemjs, requirejs or other), solution valid:
(<any>window).shop = shop;
Comments
Post a Comment