[英]Angularjs to Angular Migration: factory prototype
I am migrating from angularjs to angular.我正在从 angularjs 迁移到 angular。 I already did a lot, but I am still stuck with factories and prototypes.我已经做了很多,但我仍然坚持工厂和原型。 Card represents a play card. Card 代表一张扑克牌。
function Card() {
this.remoteId = 0;
this.version = 0;
this.name = "";
this.stars = 0;
}
Card.prototype = {
getFromRemote: getFromRemote,
initFromRemote: initFromRemote,
updateFromCard: updateFromCard,
createImage: createImage,
};
return (Card);
}
Now I am just wondering hhow to migrate it, because there are no more factories in angular.现在我只是想知道如何迁移它,因为 angular 中没有更多的工厂。 I was thinking about making the Card function a card interface like in the angular tutorial https://angular.io/tutorial/toh-pt1 .我正在考虑使卡 function 成为像 angular 教程https://angular.io/tutorial/toh-pt1中的卡接口。 Do you think that is a good idea or do I miss something?你认为这是个好主意还是我错过了什么? Any help or ideas would be appreciated!!!!任何帮助或想法将不胜感激!!!!
You can use a combination of abstract class and constructor:您可以使用抽象 class 和构造函数的组合:
abstract class
to get functionality of prototype. abstract class
以获取原型的功能。
Initial value setting logic can go inside the constructor
.初始值设置逻辑可以在go里面constructor
。 playgrond-link 游乐场链接
abstract class AbsCard {
abstract remoteId: number;
abstract version: number;
abstract name: string
abstract stars: number;
public initFromRemote(): void {
this.remoteId = 0;
}
public print() {
console.log(this);
}
}
class Card extends AbsCard {
public remoteId: number = 0;
version: number = 0;
name: string = '';
stars: number = 0;
public constructor() {
super();
/* other creation logic can come here */
}
}
let c = new Card();
c.print();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.