下面来演示一下在一个ts文件中使用另一个作用域内的变量:
Game.ts:
namespace Game {export const url: string = 'https://www.game.com';}
App.ts:
console.log(Game.url);
打包之后的代码:
Game.js:
"use strict";
var Game;
(function (Game) {
Game.url = 'https://www.game.com';
})(Game || (Game = {}));
App.js:
"use strict";
console.log(Game.url);
此时如果想要在html文件中使用编译好的js文件,一定要注意引入的顺序,要先引入Game.js ,然后再引入App.js。
从使用上来看,这种方式其实并不是推荐的。理想的方式还是通过打包工具,例如webpack等工具来实现。
