typescript - typeScript 中的 systemjs 模块 : XHR error

标签 typescript systemjs

错误:
GET http://localhost:63342/Dog.js 404(未找到)
XHR 错误(404 未找到)加载 http://localhost:63342/Dog.js

             <br/><br/>Here is my script in index.html.                                                                              <script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.src.js"></script>

<script>
    // set our baseURL reference path
    System.config({
        baseURL: '.'
    });

    System.import('Dog.js').catch(console.log.bind(console));
</script> 

动物.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);
    }
}

狗.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); 

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": false
  },
  "exclude": [
    "node_modules"
  ]
}        

以上所有文件都位于同一文件夹中。

最佳答案

尝试将您的 html 更改为:

System.config({
    baseURL: '.'
});

System.defaultJSExtensions = true;

System.import('Dog').catch(console.log.bind(console));

希望这有帮助。

关于typescript - typeScript 中的 systemjs 模块 : XHR error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35216361/

相关文章:

typescript - 构造函数中的公共(public)其余参数

angular - 在 Angular 2 TypeScript 应用程序中加载 Electron 模块

javascript - SystemJS 从目录中导入 "base script",就像在 Python 中一样

karma-runner - SystemJS 和 KarmaJS : TypeError: System. 导入不是函数

javascript - (Angular2 RC5) 如何使用 SystemJS 为每个模块创建一个可以由路由器延迟加载的包?

javascript - Angular 4 : error TS2300: Duplicate identifier 'PropertyKey'

reactjs - 如果我在 tsconfig 中使用自定义路径,Webpack Dev Server 无法找到带有 Typescript 的模块

javascript - Typescript - 导入 React 功能组件时出错

javascript - 学习者问题 : Functions for every text field. 有没有办法制作一个可以处理多个文本字段的通用函数

javascript - 在 React 中包含可重用 SVG 的最佳方式