Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将AngularJS工厂迁移到TypeScript_Angularjs_Typescript_Factory_Angularjs Factory - Fatal编程技术网

将AngularJS工厂迁移到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() {

我正在准备将AngularJS应用程序迁移到AngularJS。我目前正在考虑将JS代码转换为TS。我在组件和服务方面没有任何问题,但在工厂方面没有任何问题。我不知道如何将工厂转换为使用TypeScript

下面是一个例子:

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