【问题标题】:Loading modules in TypeScript using System.js使用 System.js 在 TypeScript 中加载模块
【发布时间】:2016-06-02 18:58:48
【问题描述】:

我有一个用 TypeScript 编写的 Node.js 和 AngularJS 应用程序,我正在尝试使用 System.js 加载模块。

如果我只导入一个类来指定一个类型,它就可以正常工作。例如:

import {BlogModel} from  "./model"
var model: BlogModel;

但是,当我尝试使用导入的类实例化变量时,我在运行时遇到异常。例如:

import {BlogModel} from  "./model"
var model: BlogModel = new BlogModel();

Uncaught ReferenceError: require is not defined

我使用 CommonJS。我不能使用 AMD,因为我有一个用于服务器端和客户端的项目。这就是我使用 System.js 在客户端加载模块的原因。

这是我的 System.js 定义:

 <script src="/assets/libs/systemjs-master/dist/system-polyfills.js"></script>
    <script src="/assets/libs/systemjs-master/dist/system.src.js"></script>

    <script>
        System.config({
            packages: {
                app: {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });
        System.import('classes.ts')
                .then(null, console.error.bind(console));
        System.import('model.ts')
                .then(null, console.error.bind(console));
    </script>

这是model.ts文件的内容:

import {User} from "./classes";
import {Post} from "./classes";

export class BlogModel{
    private _currentUser: User;
    private _posts: Post[];

    get currentUser(){
        return this._currentUser;
    }

    set currentUser(value: User){
        this._currentUser = value;
    }

    get posts(){
        return this._posts;
    }

    set posts(value: Post[]){
        this._posts = value;
    }
}

这是产生异常的 JS 行:

var model_1 = require("./model");

Uncaught ReferenceError: require is not defined

任何帮助将不胜感激!

【问题讨论】:

  • 我假设 model.ts 包含BlogModel 的定义,你能分享一下代码吗,而且你必须在主 html 页面中导入 systemjs,你也可以分享一下代码吗?理想情况下,您不必导入所有模块,它会在需要时加载,因此 System.import('classes.ts') .then(null, console.error.bind(console));应该足够了,你也不需要 ts 扩展,
  • @Madhu Ranjan 我已经编辑了原始问题并添加了model.ts的内容和System.js的脚本标签

标签: typescript systemjs


【解决方案1】:

让我们试着简化一下:

1) 如下配置你的systemjs:

System.defaultJSExtensions = true;

2) 确保您的 classes.tsmodel.tsbootstrap.ts(这是您应该创建的新文件,它将引导您的应用程序)文件被转译并位于与 index.html 相同的目录中( index.html 应该包含如上所述配置 systemjs 的脚本)

3) 在 bootstrap.ts 中:

import {BlogModel} from  "./model"
let model = new BlogModel();
console.log(model);

3) 通过调用 System.import 引导您的应用程序:

System.import('bootstrap').then(null, console.error.bind(console));

试试这些步骤,我想你会解决你的问题的。

【讨论】:

  • OK 我做到了,但是现在还有一个问题:我使用了很多 ts 文件。我用脚本元素加载它们,所以它们是在 bootstrap.ts 完成加载模块之前加载的,因此它们都会抛出引用异常。我试图通过将所有文件内容复制到 bootstrap.ts 并删除脚本元素来临时解决它。但是,我仍然遇到异常,因为我使用了 angular.js,它试图找到在 html 元素的 ng-app 属性中声明的模块。顺便说一句,如果我将脚本元素放在我配置 System.js 的元素下面,这将无济于事。
  • 删除所有脚本元素,除了 systemjs 的元素。所有加载都将由模块加载器(systemjs)根据您的 import 语句从您的引导模块(在您的情况下为 bootstrap.ts)开始。这就是 TS/JS 中的模块的全部意义所在。
  • 但是我应该如何处理像 angular.js 这样的第三方库? TypeScript 编译器说:“TS2306:'angular-1.4.7' 不是模块”。
  • 它们也会被加载器自动加载。唯一需要的是在其配置中告诉 systemjs 在哪里寻找他们的文件。例如检查这个代表:github.com/swimlane/angular1-systemjs-seed
  • 我发布了一个关于 System.js 的新问题,但没有得到任何答案。如果您也可以看看这个问题,我将不胜感激:stackoverflow.com/questions/37641653/…
猜你喜欢
  • 2016-08-29
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2016-07-13
  • 2016-02-26
  • 2012-10-10
相关资源
最近更新 更多