如何在Angular中使用cool-ascii-faces ?



只是为了好玩,我想在我的Angular应用程序中使用javascript库cool-ascii-faces。我遵循了博客"如何在Angular 8中使用外部JavaScript库"中的说明。

我做了以下的事情:

  1. 在node_modules下为我的Angular项目每npm安装cool-ascii-faces。
  2. 角。将这两个脚本添加到构建和测试部分:
"scripts": [
"./node_modules/cool-ascii-faces/cli.js",
"./node_modules/cool-ascii-faces/index.js"
]

我的app.components.ts是这样的:

import { Component, OnInit } from '@angular/core';
declare var coolFace: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.sass']
})
export class AppComponent implements OnInit {
ngOnInit(): void{
console.log(coolFace());
}
}

但是,在控制台中我只看到:ReferenceError: coolFace没有定义。我做错了什么?

感谢@Andres O. Serrano的建议。两种方法我都试过了,但都不起作用。

但我想到了另一个解决方案。只需将所有ascii面存储在/asset下的Json文件中。然后按照博客"如何在angular中读取本地JSON文件"中的说明进行操作。这招很管用。谢谢!

最新更新