我如何在Titanium中使用ES6模块

问题描述 投票:3回答:1

我在/lib/Test.js中有以下课程:

export class Test {
    constructor() {
        console.log("this is a test");
    }
}

并且在我的main.js中,我正在尝试执行以下操作:

import { Test } from "Test";
console.log(Test);

我收到以下错误消息:

TypeError: Object is not a constructor (evaluating 'new (require('/alloy/controllers/' + name))(args)')

如何使用Titanium中的ES6模块?

titanium appcelerator appcelerator-titanium
1个回答
0
投票

我使用的是SDK 8.3.0.GA,以下语法可以正常工作:

app / lib / services / myclass.js

class MyClass {
  constructor(prop1) {
    this.prop1 = prop1;
  }

  get something() {
    return this.calcSomething();
  }

  calcSomething() {
    return this.prop1 * 2;
  }
}

module.exports = MyClass;

然后在app / controllers / index.js中

import MyClass from 'services/myclass';

let myClass = new MyClass(2);
alert(myClass.something);

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.