打字稿:找不到模块 angular
Typescript: Cannot find module angular
我正在使用 angular-cli (1.0.0-beta.10) 开发 angular 2 项目,并且想使用这个库 (https://github.com/Glench/fuzzyset.js)。当我尝试导入它时,Typescript 不接受它作为一个模块,我真的不知道为什么。它给我 Cannot find module
错误。
作为解决方法,我尝试在我的 system-config.ts
文件中将其声明为模块
declare module "fuzzyset" {}
我正在像这样将其导入我的组件之一
import * as FuzzySet from 'fuzzyset';
并添加到系统配置:
System.config({
paths: {
// paths serve as alias
'npm:': 'vendor/'
},
map: {
'fuzzyset': 'npm:fuzzyset.js/index.js',
},
packages: cliSystemConfigPackages
});
但现在它给出 Cannot invoke an expression whose type lacks a call signature.
为了解决这个错误,我将模块声明更改为:
declare module "fuzzyset" {
export function FuzzySet(options?:any): any;
}
这确实以编译所有内容的方式工作,但是我在运行时遇到如下错误:
Browser console errors
谁能帮我解决这个问题?
提前致谢:)
只需简单地使用:
in app.ts
import fuzzyset from 'fuzzyset' (app.ts)
in config.ts
map: {
'fuzzyset': 'npm:fuzzyset.js/lib/fuzzyset.js',
}
class definition,
export class App {
a: any; // fuzzyset
printThis: array;
name:string;
constructor() {
// start doing fuzzy stuff
this.a = FuzzySet()
this.a.add("michael axiak");
this.printThisVar = this.a.get("micael asiak");
// end doing fuzzy stuff
this.name = 'Angular2'
}
}
in @component:
<p>{{printThisVar}}</p>
Working Plnkr here
我可以解决这个问题。它有一些模块定义问题,Typescript 没有将其识别为模块。修改库中的定义确实有效,但这不是正确的方法。在不修改库的情况下工作的最终解决方案是在 typings.d.ts
中添加它的定义。如果您想查看代码:
declare module "fuzzyset" {
function FuzzySet(options?: FuzzySet.FuzzySetOptions) : any;
namespace FuzzySet {
interface FuzzySetOptions {
arr?: any;
useLevenshtein?: any;
gramSizeLower?: any;
gramSizeUpper?: any;
}
}
export = FuzzySet;
}
我正在使用 angular-cli (1.0.0-beta.10) 开发 angular 2 项目,并且想使用这个库 (https://github.com/Glench/fuzzyset.js)。当我尝试导入它时,Typescript 不接受它作为一个模块,我真的不知道为什么。它给我 Cannot find module
错误。
作为解决方法,我尝试在我的 system-config.ts
文件中将其声明为模块
declare module "fuzzyset" {}
我正在像这样将其导入我的组件之一
import * as FuzzySet from 'fuzzyset';
并添加到系统配置:
System.config({
paths: {
// paths serve as alias
'npm:': 'vendor/'
},
map: {
'fuzzyset': 'npm:fuzzyset.js/index.js',
},
packages: cliSystemConfigPackages
});
但现在它给出 Cannot invoke an expression whose type lacks a call signature.
为了解决这个错误,我将模块声明更改为:
declare module "fuzzyset" {
export function FuzzySet(options?:any): any;
}
这确实以编译所有内容的方式工作,但是我在运行时遇到如下错误: Browser console errors
谁能帮我解决这个问题? 提前致谢:)
只需简单地使用:
in app.ts
import fuzzyset from 'fuzzyset' (app.ts)
in config.ts
map: {
'fuzzyset': 'npm:fuzzyset.js/lib/fuzzyset.js',
}
class definition,
export class App {
a: any; // fuzzyset
printThis: array;
name:string;
constructor() {
// start doing fuzzy stuff
this.a = FuzzySet()
this.a.add("michael axiak");
this.printThisVar = this.a.get("micael asiak");
// end doing fuzzy stuff
this.name = 'Angular2'
}
}
in @component:
<p>{{printThisVar}}</p>
Working Plnkr here
我可以解决这个问题。它有一些模块定义问题,Typescript 没有将其识别为模块。修改库中的定义确实有效,但这不是正确的方法。在不修改库的情况下工作的最终解决方案是在 typings.d.ts
中添加它的定义。如果您想查看代码:
declare module "fuzzyset" {
function FuzzySet(options?: FuzzySet.FuzzySetOptions) : any;
namespace FuzzySet {
interface FuzzySetOptions {
arr?: any;
useLevenshtein?: any;
gramSizeLower?: any;
gramSizeUpper?: any;
}
}
export = FuzzySet;
}