Loading amd modules using typescript

Source: Internet
Author: User
Tags export class

If you have understood nodejs, you should be familiar with the following syntax:

Math. js

exports.add = function(n1, n2){return n1 + n2;}

Server. js

var math = require('./math.js');math.add(1,2);

In this way, you can import JS files in JS files. However, in web development, we usually need to import all JS files in order on the HTML page. Similar methods are also provided in Typescript.

Compoment. Ts

export class Compoment {    private element: Element;    private id: string;    constructor (public config: Object) {            }    initialize() {         }    initCompoment() {             }}

Panel. Ts

import mod = module("Compoment");class Panel extends mod.Compoment{    constructor (config) {         super(config);        console.log(this.config);    }}

Next, use TSC panel during compilation. TS cannot achieve the desired effect. You need to use the amd module to compile the TSC -- module amd panel. TS and add require to the HTML page. JS and specify the entry.

<script data-main="panel" type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.1/require.min.js"></script>

Then we can test it in the browser.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.