相关疑难解决方法(0)

使用System.js在TypeScript中加载模块

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

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

import {BlogModel} from  "./model"
var model: BlogModel;
Run Code Online (Sandbox Code Playgroud)

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

import {BlogModel} from  "./model"
var model: BlogModel = new BlogModel();
Run Code Online (Sandbox Code Playgroud)

未捕获的ReferenceError:未定义require

我使用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>
Run Code Online (Sandbox Code Playgroud)

这是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 …
Run Code Online (Sandbox Code Playgroud)

typescript systemjs

10
推荐指数
1
解决办法
9120
查看次数

使用TypeScript在客户端和服务器之间共享模块

我想编写一个可在node.js服务器和浏览器之间重用的可移植模块.

这是阻止我的模块化事物.我正在阅读http://caolanmcmahon.com/posts/writing_for_node_and_the_browser/,它看起来很简单,但是让tsc生成这样的东西可能吗?

client module typescript

4
推荐指数
1
解决办法
1325
查看次数

标签 统计

typescript ×2

client ×1

module ×1

systemjs ×1