gpt4 book ai didi

javascript - typescript 中的 Ember js 观察者和计算属性

转载 作者:搜寻专家 更新时间:2023-10-30 21:43:29 25 4
gpt4 key购买 nike

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

我需要用 typescript 编写以下代码。

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));

关于javascript - typescript 中的 Ember js 观察者和计算属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com