如何在TypeScript中使用systemjs导入和导出模块?

如何在TypeScript中使用systemjs导入和导出模块?

问题描述:

有谁知道如何在TypeScript中使用systemjs导出模块?我得到了错误:TS1148 cannot compile modules unless the '--module' flag is provided如何在TypeScript中使用systemjs导入和导出模块?

这是我的代码; animal.ts

export class Animal { 
    color: string; 
    age: number; 

    constructor(color: string, age:number) { 
     this.color = color; 
     this.age = age; 
    } 

    add(animal: Animal) { 
     return new Animal(this.color + animal.color, this.age + animal.age); 
    } 
} 

dog.ts

import {Animal} from './animal'; 

var animal1 = new Animal("red", 1); 
var animal2 = new Animal("yellow", 2); 
var animal3 = animal1.add(animal2); 
console.log('Combine Two Animals' + animal3); 

您可以在您的系统配置指定打字稿编译器选项。有很多选项可以找到here

System.config({ 
    typescriptOptions:{ 
     module: 'system' 
    } 
}); 
+0

谢谢。我将以下代码添加到tsconfig.json“compilerOptions”中:{ “target”:“ES5”, “module”:“system”}然后它就起作用了。 –

+0

但是接着出现另一个错误:系统未定义。你知道如何解决它吗?我是否需要将animal.js和dog.js文件包含到我的html文件中?我是新来的TypeScript。你知道有什么好的资源可以帮助我使用TypeScript和systemjs吗?看来大多数网站使用nodejs导入模块,我不知道使用systemjs导入模块的好处吗?谢谢。 –

+0

浏览器无法导入模块,这就是为什么存在SystemJS,webpack,browserify,requirejs等。我建议阅读SystemJS回购文件。您可能已经在您的html中忘记了系统的'