【问题标题】:How do I work with ES6 modules in Titanium如何在 Titanium 中使用 ES6 模块
【发布时间】:2017-11-09 04:24:07
【问题描述】:

我在/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】:

    我正在使用 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);
    

    希望对你有帮助!

    【讨论】:

    • 感谢您告诉我。这篇文章已经很老了,所以我还没有在较新的 SDK 上测试它——再试一次。
    • 是的,我已经意识到这是一个旧帖子,但只是想让您/任何有兴趣的人知道这是可能的。
    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2020-10-25
    • 2022-08-20
    • 2017-09-07
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多