How do I work with ES6 modules in Titanium

Viewed 248

I have the following class inside /lib/Test.js:

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

and in my main.js I'm trying to do the following:

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

I'm getting the following error message:

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

How can I work with ES6 modules in Titanium?

1 Answers

I'm using SDK 8.3.0.GA and the following syntax works just fine:

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;

and then in app/controllers/index.js

import MyClass from 'services/myclass';

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

Hope it helps!

Related