我正在尝试在Angular2 webpack项目中使用ts automapper libary,但不确定如何导入或包含它。如何实现这一点的一个例子将是伟大的。
答案 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。
简而言之:
现在您应该能够使用所记录的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)
使用yarn或npm安装“ automapper-ts”软件包
导入包:
导入“ automapper-ts”;
按照文档中所述使用autommaper:
automapper.initialize(...)或automapper.createMap(...)等。