最近,我一直在尝试在angular 8应用程序中使用UNDERSCORE库,但是我得到了警告,在本地声明'_',但未导出当我尝试在最近创建的HelperService中使用它时。
对于安装,我尝试过: npm安装下划线 也 npm install -g typescript @ next 接着 npm install-保存下划线 npm install --save @ types / underscore
警告在带下划线的导入中显示 从“下划线”导入{_};
import { _ [JUST RIGHT HERE] *} *来自“下划线”; **
这是HelperService的完整代码:
import { Injectable } from '@angular/core';
import { _ } from 'underscore';
@Injectable({
providedIn: 'root'
})
export class HelperService {
constructor() { }
public each(array, delegate) {
return _.each(array, delegate);
}
public without(array, delegate) {
return _.without(array, delegate);
}
}
此服务位于路径中:
*用户//工作区//src/services/helper/helper.service.ts
这是完整的警告: 模块“ ../../../../../../../../Users//workspace//node_modules/@types/underscore”在本地声明“ ”,但是它没有被导出.ts(2459) index.d.ts(14,13):''在这里声明。 index.d.ts(24,16):和这里。
我怎样才能正确识别'underscore'中的导入{_}??
我很感谢以此方式提供的帮助
答案 0 :(得分:0)
尝试使用此语法-
import { * as _ } from 'underscore';
答案 1 :(得分:0)
在您的declare module 'underscore';
文件中添加index.d.ts
答案 2 :(得分:0)
您不再需要大括号,它现在不是导出成员而是默认成员。您可以直接导入:
import _ from 'underscore';
答案 3 :(得分:0)
TypeScript 是 JavaScript 的类型化超集,可编译为纯 JavaScript。 TypeScript 有自己的语法、函数和变量可以定义类型,如果要使用外部库如 underscore.js
则需要为 TypeScript 声明类型定义。一些库包括输入文件,你不需要为它们安装 TypeScript 的类型目标。但如果某个库没有 .d.ts 文件,则需要安装它。
Type Search
解决方案:
导入库
npm install underscore --save
npm install @types/underscore --save
在 tsconfig.app.json 中,向数组 'types' 添加下划线:
"types": [ "underscore" ]
用法
import * as _ from 'underscore';
答案 4 :(得分:-1)
使用此
import * as _ from 'underscore';