【问题标题】:Including Typescript code in Relay (System.js import)在 Relay 中包含 Typescript 代码(System.js 导入)
【发布时间】:2016-02-10 02:36:14
【问题描述】:

如何包含 system.js 来修复以下错误?还是有其他解决办法?

我下载了 relay-starter-kit (https://github.com/relayjs/relay-starter-kit),将 database.js 更改为 database.ts,内容如下(片段 1)。

我运行“npm run update-schema”并得到错误

System.register([], function (exports_1) {
^

ReferenceError: System is not defined
    at Object.<anonymous> (database.js:9:1)
    at Module._compile (module.js:410:26)
..

我知道这是因为 update-schema 使用 scripts/updateSchema.js -> data/schema.js -> 从 data/database.js(database.ts 的编译版本)导入对象 -

System.register([], function(exports_1) {

片段 1:

/// <reference path="./interface.d.ts" />

export class User implements IUser{
    constructor (public id: String, public name: String){
        this.id = id;
        this.name = name;
    }
}
// Model types
class UserModel extends User implements IUserModel {

     constructor(public id: String, public name: String){
         super (id,name);
     }
     getUser ():IUser{
         return this;
     }
     setUser (_User:IUser) : void {
         this.id = _User.id;
         this.name = _User.name;
     }  
    getUserbyId (_id:String):IUser{
        if (_id === this.id){
            return this;
        } else {
            return null;
        }    
    }  

}

export class Widget implements IWidget{
    constructor (public id: String, public name: String){
        this.id = id;
        this.name = name;
    }
}
// Model types
class WidgetModel extends Widget implements IWidgetModel {

     constructor(public id: String, public name: String){
         super (id,name);
     }
     getWidget ():IWidget{
         return this;
     }
     setWidget (_Widget:IWidget) : void {
         this.id = _Widget.id;
         this.name = _Widget.name;
     }
    getWidgetbyId (_id:String):IWidget{
        if (_id === this.id){
            return this;
        } else {
            return null;
        }    
    }       
}

// Mock data
var viewer:IUserModel = new UserModel('1','Anonymous');

var widgets:IWidget[] = ['What\'s-it', 'Who\'s-it', 'How\'s-it'].map((name:String, i:any) => {
  let widget:IWidgetModel = new WidgetModel(name,`${i}`);
  return widget;
});
  export function getUser (_id:String):IUser {
          return viewer.getUserbyId(_id);
  } 
  export function getViewer ():IUser {
      return viewer.getUser();
  }
  export function getWidget (_id:String):any {
      widgets.forEach(
          w => {
              if (w.id === _id) 
                  return w;
              else 
                  return null;
          }
      );
  }
  export function getWidgets (): IWidget[]{
      return widgets;
  }

【问题讨论】:

    标签: javascript typescript systemjs relay


    【解决方案1】:

    tsconfig.json 有

    “模块”:“系统”,

    改成

    “模块”:“umd”,

    它成功了。

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 2019-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      • 2017-10-15
      • 2017-02-05
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多