我正在使用系统模块选项将 typescript 模块转换为 javascript。我正在浏览器中执行此操作。
当初始化由 typescript 生成的模块类的代码也使用 systemjs system.import 加载时,我可以使用此模块。
我如何使作为模块一部分的类可从未通过 systemjs 加载的 javascript 代码使用?例如。在页面中嵌入javascript?
您可以使用SystemJS在 Javascript 中导入模块。
假设你有一个名为app.ts导出一个名为value.
app.ts:
export let value = 'ABCASF';
在脚本标签中你可以写:
System.import('app.js').then(function(appModule) {
console.log(appModule.value);
}, console.error.bind(console));
请记住,您必须提供的模块名称系统导入可能会根据您的设置而有所不同。
TypeScript 类被转换为 ES5 函数,因此您可以通过这种方式在 javascript 中使用它们。
示例.ts:
export class Example {
constructor(public someValue: string, private someOtherValue: number) {
}
public method() {
return this.someOtherValue;
}
}
在脚本标签中这样:
System.import('example.js').then(function(example) {
// Example class is a function on the module
// use new to make a new instance
var value = new example.Example('a', 5);
// work as usual
console.log(value.method());
}, console.error.bind(console));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)