网站搜索

javascript - 将 ng2-charts 添加到 Typescript/Angular 2 项目中,引用路径和 .js 文件路径不正确


我对于如何将像 ng2-charts 这样的 npm 包添加到我的 Angular2 TypeScript 项目中有些困惑。

通过添加:

"ng2-charts": "1.0.3"

到我的 package.json 文件,它安装得很好,当我将 'ng2-charts/ng2-charts' 添加到我的 TypeScript 页面时:

import {Component, Input, OnInit} from 'angular2/core';
import {Router, RouterLink, RouteParams} from 'angular2/router'
import {CHART_DIRECTIVES} from 'ng2-charts/ng2-charts';

@Component({
    selector: 'detailMatch',
    templateUrl: './app/components/match/detail.html',
    directives: [RouterLink, CHART_DIRECTIVES]
})

它也能完美运行。在 Visual Studio 中,当我输入第一个字母等时,会自动找到名称 CHART_DIRECTIVE,但当我尝试加载页面时,我得到:

http://localhost:5000/ng2-charts/ng2-charts.js 404 (Not Found)

这是因为我在那个位置没有 ng2-charts:

它存储在 "lib\js\ng2-charts\ng2-charts.js" 中,就像我其他所有的 Angular、http、bootstrap、router、Rx 等的 js 文件一样。

那么我如何告诉 Angular .js 文件的位置在:

 http://localhost:5000/lib/js/ng2-charts/ng2-charts.js

答案#1

我不认为你正在使用任何模块加载器。将 ng2-charts.js 文件在 angular2 库之后添加到脚本引用中,看看是否能工作

<script src="/lib/js/ng2-charts/ng2-charts.js"></script>