如何使用Typescript定义emberjs观察者和计算属性?

我需要在打字稿中编写以下代码。

module App {
App.UserController = ember.ObjectController.extend({
    firstName: "John",
    lastName: "Doe",
    fullName: function() {
       return this.get("firstName") + " " + this.get("lastName");
    }.property("firstName", "lastName")
});
}

就像是:
class UserController extends ember.ObjectController {
    firstName: string;
    lastName: string;

    constructor() {
        super();
        this.firstName = "John";
        this.lastName = "Doe";
    }

    get fullName(): string {
        return this.firstName + " " + this.lastName;
    }.property("firstName", "lastName")
}

但这似乎不起作用。有人可以告诉我不使用JavaScript的正确方法吗?

最佳答案

这是将要编译的代码版本:

module App {

    export var UserController = Ember.ObjectController.extend({
        firstName: "John",
        lastName: "Doe",
        fullName: function () {
            return this.get("firstName") + " " + this.get("lastName");
        }.property('model.isCompleted')
    });
}

// Example:
var x = App.UserController;

更新资料

您可以使用一个类来实现,这是一个编译示例:
class UserController extends Ember.ObjectController {
    private firstName: string;
    private lastName: string;

    constructor() {
        super();
        this.firstName = "John";
        this.lastName = "Doe";
    }

    get fullName(): string {
        return this.firstName + " " + this.lastName;
    }
}

var x = new UserController();

console.log(JSON.stringify(x));

09-19 12:56