如何将资源从资源加载到Angular2项目?

时间:2018-01-25 12:45:38

标签: angular

我想从assets导入一个脚本。我在index.html执行此操作

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>...</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!--npm package : image-compressor-->
  <script src="/assets/image-compressor/image-compressor.js"></script>
  <script src="/assets/material-components-web/dist/material-components-web.js"></script>
</head>
<body>
  <app-root>
    Loading...
  </app-root>
</body>

并在app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = '...';
  ngOnInit(){
    var imageCompressor = new ImageCompressor(); //create a ImageCompressor from image-compreeor package
    console.log(imageCompressor)
    ...
  }
}

然后我收到了一个错误,该错误一直在运行,虽然错误,网站仍然运行并显示了app.component.ts上的ImageCompressor。 当我构建项目时ng build会出现同样的错误

ERROR in src/app/app.component.ts(??,31): error TS2552: Cannot find name 'ImageCompressor'. Did you mean 'imageCompressor'?

我该怎么办?

谢谢!

1 个答案:

答案 0 :(得分:0)

添加image-compressor.js文件的方式,编译器不知道

尝试添加

declare var ImageCompressor: any; 

在app.component.ts中声明组件之前