将Automapper导入angular2网页包项目

将Automapper导入angular2网页包项目,angular,webpack,Angular,Webpack,我试图在Angular2网页包项目中使用,但不确定如何导入或包含它。举个例子来说明如何做到这一点是很好的 也许有更好的方法来使用这个库,但是,我在我的项目中所做的是 /** test.component.ts ** Please note that you need to change Path as this is referenced to my node_module and probably it's different in your end. **/ ///

我试图在Angular2网页包项目中使用,但不确定如何导入或包含它。举个例子来说明如何做到这一点是很好的

也许有更好的方法来使用这个库,但是,我在我的项目中所做的是

   /** test.component.ts
    ** Please note that you need to change Path as this is referenced to my node_module and probably it's different in your end. 
   **/

/// <reference path="../../../../node_modules/automapper-ts/dist/automapper.d.ts" />  

import {Component, OnInit} from '@angular/core';

@Component({
  selector: 'test-component',
})
export class TestComponent implements OnInit {
  private jsonObj;
  constructor() {
    automapper.map("JSON", "newType", this.jsonObj);
  }

  ngOnInit() {
  }
}


我希望你已经找到了一个有效的解决办法。与您所问的完全相同的问题也已在库的GitHub页面上发布和回答:

简言之:

  • 使用npm:npm Install automapper ts--save安装automapper ts库
  • 在文件typings.d.ts中添加以下行:声明模块“automapper ts”
  • 将automapper ts minified JS文件添加到angular-cli.json中的脚本数组:“../node_modules/automapper ts/dist/automapper.min.JS”
  • 在模块/组件文件中导入步骤2中创建的模块:从“automapper ts”导入{}

现在,您应该能够使用文档中的
automapper
全局变量。

我的两分钱用于此对话:

在tsconfig.json文件中,确保typings.d.ts文件的路径正确

“类型根”:[ “节点\模块/@类型”, “./src/typings.d.ts” ],

否则,您可能会出现一些令人讨厌的“未定义自动映射”错误

  • 使用纱线或npm安装“automapper ts”包装

  • 进口包装:

    导入“自动映射ts”

  • 按照文档中的说明使用autommaper:

    automapper.initialize(…)或automapper.createMap(…)等


  • 谢谢你的建议。你在用网页包吗?如果你像这样引用它,你在automapper中获得了所有的intellisense吗?是的,我正在使用Webpack,就我的工作而言,是的,它工作正常。试一试。我想在您的模块/组件
    中添加“automapperts/dist/automapper”
    
    "moduleResolution": "node",
    
    "moduleResolution": "classic",