-->

How to load external AMD module using Typescript 1

2019-08-09 05:53发布

问题:

I'm using VS2015 and I've created a TypeScript HTML application. I also made the simplest possible external module, like this.

export class MyClass {
    public Test(): string {
        return "Erik";
    };

    constructor() {
    };
};

Then in my app.ts file i wrote the following.

import E = require('./Erik');

window.onload = () => {
    let c: E.MyClass = new E.MyClass();
    alert(c.Test());
};

Using F12 dubugging in Chrome I can see this error.

Uncaught ReferenceError: define is not defined

What am I missing? I have made sure that AMD moduletype is selected in the project settings and a Erik.js file is created when compiling.

回答1:

You still need to use a module loader library that works with AMD modules.

For example, you could include require.js in your application, then do something along these lines:

<script data-main="scripts/app" src="scripts/require.min.js"></script>

Where data-main is the path to app.js.