gpt4 book ai didi

angular - 如何在 Angular 2 中使用 underscore.js 库

转载 作者:太空狗 更新时间:2023-10-29 16:50:20 26 4
gpt4 key购买 nike

我试图创建一个带有 Angular 2 的应用程序,并希望在我的 .ts 文件中使用 underscore.js 库,例如当我想使用此功能时:

   let myId = _.rest([5, 4, 3, 2, 1]);

_ 没有定义并抛出错误,我不想在我的模块中使用 declare var _ : any;

最佳答案

对于基于 https://cli.angular.io 的项目,我需要执行以下操作:

1)导入库

npm install underscore --save
npm install @types/underscore --save

2) 在 tsconfig.app.json 中,为数组 'types' 添加下划线:

"types": [
"underscore"
]

3) 在我需要使用下划线的任何组件文件中,我添加这个

import * as _ from 'underscore';

4) 然后我可以使用:

console.log('now: ', _.now());

http://underscorejs.org的所有功能

关于angular - 如何在 Angular 2 中使用 underscore.js 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37569537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com