将AngularJS工厂迁移到TypeScript
我正在准备将AngularJS应用程序迁移到AngularJS。我目前正在考虑将JS代码转换为TS。我在组件和服务方面没有任何问题,但在工厂方面没有任何问题。我不知道如何将工厂转换为使用TypeScript 下面是一个例子:将AngularJS工厂迁移到TypeScript,angularjs,typescript,factory,angularjs-factory,Angularjs,Typescript,Factory,Angularjs Factory,我正在准备将AngularJS应用程序迁移到AngularJS。我目前正在考虑将JS代码转换为TS。我在组件和服务方面没有任何问题,但在工厂方面没有任何问题。我不知道如何将工厂转换为使用TypeScript 下面是一个例子: (function() { 'use strict'; angular.module('MyApp') .factory('Fruit', Fruit); /** @ngInject */ function Fruit() {
(function() {
'use strict';
angular.module('MyApp')
.factory('Fruit', Fruit);
/** @ngInject */
function Fruit() {
var Fruit = function(dto) {
// dto properties
this.id = "";
this.name = "";
this.type = "Orange";
this.color = "#000000";
//--------------
// Create from API response DTO
if (dto) {
this.id = dto.id;
this.name = dto.data.name;
this.type = dto.data.type;
this.color = dto.data.color;
}
};
return Fruit;
}
})();
我试过了,但没用。我得到dto->dtoProvider未找到
(function() {
'use strict';
angular.module('MyApp')
.factory('Fruit', class Fruit {
// dto properties
public id = "";
public name = "";
public type = "Orange";
public color = "#000000";
constructor (private dto: any) {
// Create from API response DTO
if (dto) {
this.id = dto.id;
this.name = dto.data.name;
this.type = dto.data.type;
this.color = dto.data.color;
}
}
})
})();
另外,我还没有能力导入/导出类。将
水果
函数放在类构造函数中,并返回它:
class Fruit {
constructor () {
function Fruit(dto) {
// dto properties
this.id = "";
this.name = "";
this.type = "Orange";
this.color = "#000000";
//--------------
// Create from API response DTO
if (dto) {
this.id = dto.id;
this.name = dto.data.name;
this.type = dto.data.type;
this.color = dto.data.color;
};
}
return Fruit;
}
}
angular.module("myApp",[])
.factory("Fruit", Fruit);
.run(function(Fruit) {
var x = new Fruit();
console.log(x);
})