[TypeScript] Loading Compiled TypeScript Files in Browser with SystemJS

本文详细介绍如何将TypeScript编译后的JavaScript文件通过SystemJS加载到浏览器中运行。包括设置index.html页面引入SystemJS,配置SystemJS加载指定的模块,启动HTTP服务器验证效果等。

TypeScript outputs JavaScript, but what are you supposed to do with it? This lesson shows how to take the output and use SystemJS as the module loader so that you can use the files in your browser.

 

To use system.js, first create a index.html, add system.js file from npmcdn:
https://npmcdn.com/systemjs@0.19.29/

 

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://npmcdn.com/systemjs@0.19.29/dist/system.js"></script>
</head>
<body>

<script>
    System.config({
        packages: {
            "dist": {
                "defaultExtension": "js",
                "main": "main"
            }
        }
    });

    System.import("dist")
</script>
</body>
</html>

 

It tell System to load the packages. Serve "dist" folder, use default extension as js, and the main entry file is main.js. Then import dist folder.

 

Start a server:

http-server -c-1  // -c-1 -> no cache

 

Run: 

tsc -w

 

To prove it do work in broswer, add console log in the code:

//main.ts

import {Two} from './two';

class Person{
    constructor(){
        console.log("Person!");
    }
}

new Two();
new Person();


// tow.ts
export class Two{
    constructor(){
        console.log("Two");
    }
}

 

 

转载于:https://www.cnblogs.com/Answer1215/p/5571892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值