How to export a TypeScript module for SystemJS?

ぐ巨炮叔叔 提交于 2019-12-05 22:53:21

I think I managed to reproduce your example.

It looks like with this setup, the file - MyFancyModule.ts - is already a module in itself. When you do

export module MyFancyModule {

you create a nested module within it.

This code should work with everything else unchanged:

    SystemJS.import('MyFancyModule').then(function(MyFancyModule) {
        var myFancyInstance = new MyFancyModule.MyFancyModule.MyFancyClass('Test');
        console.log(myFancyInstance.text);
    });

Alternatively, you can remove export module from the .ts file and have export class at the top level - then your original import will work.

Typescript takes care of how to load the modules from the different module systems that it supports.
The idea is that you have a specific syntax of doing that (in typescript) and based on the module you specify to the compiler (for example in the tsconfig.json) it will generate the appropriate code for loading the modules according to the selected module system.

So all you need to do is:

import * as MyFancyModule from "MyFancyModule";

var myFancyInstance = new MyFancyModule.MyFancyClass('Test');
console.log(myFancyInstance.text);

Edit

The compiler should generate something like:

System.register(["MyFancyModule"], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var MyFancyModule;
    var myFancyInstance;
    return {
        setters:[
            function (MyFancyModule_1) {
                MyFancyModule = MyFancyModule_1;
            }],
        execute: function() {
            myFancyInstance = new MyFancyModule.MyFancyClass('Test');
            console.log(myFancyInstance.text);
        }
    }
});

As you can see there are no traces of this import from syntax in the compiled js.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!