在Angular中导入JS库

在Angular中导入JS库,angular,Angular,我正在尝试在一个角度项目中使用外部库。 这是我的文件 如果您使用的是npm,则可以将Font Face Observer作为依赖项安装: $ npm install fontfaceobserver You can then require fontfaceobserver as a CommonJS (Browserify) module: var FontFaceObserver = require('fontfaceobserver'); var font = new FontFace

我正在尝试在一个角度项目中使用外部库。 这是我的文件

如果您使用的是npm,则可以将Font Face Observer作为依赖项安装:

$ npm install fontfaceobserver
You can then require fontfaceobserver as a CommonJS (Browserify) module:

var FontFaceObserver = require('fontfaceobserver');

var font = new FontFaceObserver('My Family');

font.load().then(function () {
  console.log('My Family has loaded');
});

库是使用
require
导入的,但angular不喜欢该关键字。是否有一些标准的导入库的方法?

如果是它的网页,您应该可以使用es6导入来导入它。刚刚安装,这对我很有用:

从“FontFaceObserver”导入FontFaceObserver

this.font=new-FontFaceObserver('ariel')

此.font如下所示:

this.font = {
family:"ariel",
stretch:"normal",
style:"normal",
weight:"normal"
}

下面是在组件中导入的方法

import FontFaceObserver from 'fontfaceobserver';

export class AppComponent {

  public fontFace: any;

  ngOnInit() {
    this.fontFace = new FontFaceObserver('ariel');
  }
}

您是否尝试过:
导入“fontfaceobserver”
,如果您的
fontfaceobserver
位于
node_modules
文件夹中,并且可以正常工作。基本上谢谢,如果是在node_模块中,那么您可以使用import关键字吗?