将Automapper导入angular2 webpack项目

时间:2016-10-19 17:48:44

标签: angular webpack

我正在尝试在Angular2 webpack项目中使用ts automapper libary,但不确定如何导入或包含它。如何实现这一点的一个例子将是伟大的。

4 个答案:

答案 0 :(得分:2)

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

   /** 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() {
  }
}

请记住,我修改了tsconfig.json文件以使其发挥作用。

"moduleResolution": "node",

"moduleResolution": "classic",

答案 1 :(得分:1)

我希望你找到了一个有效的解决方案。您要问的完全相同的问题已在图书馆的GitHub页面上发布并回答:https://github.com/loedeman/AutoMapper/issues/17

简而言之:

  • 使用npm安装automapper-ts库:npm install automapper-ts --save
  • 将以下行添加到文件typings.d.ts:declare module&#39; automapper-ts&#39;;
  • 将automapper-ts缩小的JS文件添加到angular-cli.json中的scripts数组中:&#34; ../ node_modules / automapper-ts / dist / automapper.min.js&#34;
  • 导入模块/组件文件中步骤2中创建的模块:来自&#39; automapper-ts&#39;;
  • 的import {}

现在您应该能够使用所记录的automapper全局变量。

答案 2 :(得分:1)

我对这次谈话的两分钱:

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

  

&#34; typeRoots&#34;:[         &#34; node_modules / @类型&#34 ;,         &#34; ../的src / typings.d.ts&#34;       ],

否则你可能会遇到一些讨厌的问题&#34;自动播放器未被定义&#34;错误。

答案 3 :(得分:1)

  1. 使用yarn或npm安装“ automapper-ts”软件包

  2. 导入包:

      

    导入“ automapper-ts”;

  3. 按照文档中所述使用autommaper:

      

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