简体   繁体   English

角度错误:WordCloud 不是函数

[英]angular error: WordCloud is not a function

i am still prety new to angular, i was trying to use wordcloud2 in my app and got an error WordCloud is not a function when trying to call the function.我对 angular 还是很陌生,我试图在我的应用程序中使用 wordcloud2 并在尝试调用该函数时遇到错误 WordCloud 不是函数。

wordcloud2.js contains the WordCloud function, I tried to import it, but I encoded the problem described above, i think i am missing something, i think the declaration is recognized but not the function. wordcloud2.js 包含 WordCloud 函数,我尝试导入它,但是我对上述问题进行了编码,我想我遗漏了一些东西,我认为声明被识别但函数不被识别。

please see this code example attached:请参阅附加的此代码示例:

https://stackblitz.com/edit/angular-7hsahs https://stackblitz.com/edit/angular-7hsahs

angular error: WordCloud is not a function角度错误:WordCloud 不是函数

Change line 2 from将第 2 行从
import * as WordCloud from 'wordcloud';
to:到:
import WordCloud from 'wordcloud';

The difference between the two import statements is that the one you used will import the whole module as one object, and the WordCloud() function is a property of it.两个 import 语句的区别在于你使用的那个将整个模块作为一个对象导入,而WordCloud()函数是它的一个属性。 That is not supported by the type definition, so TypeScript will throw an error, since you incorrectly told TypeScript the type of the imported module.类型定义不支持,因此 TypeScript 会抛出错误,因为您错误地告诉 TypeScript 导入模块的类型。
If you instead import it with import WordCloud from 'wordcloud';如果您改为使用import WordCloud from 'wordcloud';导入它import WordCloud from 'wordcloud'; , you instead get only the function that is described as being exported by default in the type declaration. ,您只会获得在类型声明中被描述为默认导出的函数。

Basically you have several different forms of exports:基本上你有几种不同形式的出口:

export function foo() {return "bar";}
export const BAR = "foo";

Here, you can import the different variables like this:在这里,您可以像这样导入不同的变量:

import {foo, BAR} from './foobar';
console.log(foo(), BAR); // "bar", "foo"

or this:或这个:

import * as foobar from './foobar';
console.log(foobar.foo(), foobar.BAR); // "bar", "foo"

or you have an export with a default export like this:或者你有一个像这样默认导出的导出:

export default function foo() {return 'bar';}

in which case you can import like this:在这种情况下,您可以像这样导入:

import foo from './foo';
console.log(foo()); // "bar"

or this:或这个:

import {default as foo} from './foo';
console.log(foo()); // "bar"

Although, if a default export is given, you can name the imported variable anything you want.虽然,如果给出了默认导出,您可以随意命名导入的变量。 In the example above, import bar from '.\\foo';在上面的例子中, import bar from '.\\foo'; would work, too, and invoking bar() would return "bar" .也可以,调用bar()会返回"bar" That is because the exported variable basically does not have its name exported, but has the special name "default" instead.那是因为导出的变量基本上没有导出其名称,而是具有特殊名称“default”。

You can read up a bit more about importing in TypeScript here:您可以在此处阅读有关在 TypeScript 中导入的更多信息:
https://blog.jdriven.com/2017/06/typescript-and-es6-import-syntax/ https://blog.jdriven.com/2017/06/typescript-and-es6-import-syntax/

Try to change line 49 like this:尝试像这样更改第 49 行:

WordCloud.default(document.getElementById("my_canvas"), {
            list: temp});

in the stackblitz which you provide I saw 'default' function in WordClound namespace.在您提供的 stackblitz 中,我在 WordClound 命名空间中看到了“默认”函数。 Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM