【发布时间】:2018-06-18 18:53:49
【问题描述】:
我目前正在开发一个 Angular4 应用程序,我想导入一些 javascript 库,但仅用于单个组件。
目前我可以通过在.angular-cli.json 中定义它们的路径来轻松导入这些库:
{
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/jqueryui/jquery-ui.js",
"../src/assets/js/jquery/cometd/org-cometd.js",
"../src/assets/js/jquery/cometd/jquery.cometd.js",
],
...
}
但是,将为所有应用程序导入上述脚本。有没有办法只为特定组件导入它们? 我尝试将它们导入到组件中,如下所示,但没有成功。
import { Component, OnInit, ViewEncapsulation, Inject } from '@angular/core';
...
import "../../../../../node_modules/jquery/dist/jquery.min.js";
import "../../../../../node_modules/jqueryui/jquery-ui.js";
import "../../../../assets/js/jquery/cometd/org-cometd.js";
import "../../../../assets/js/jquery/cometd/jquery.cometd.js";
@Component({
selector: '...',
templateUrl: '...',
styleUrls: '...',
encapsulation: ViewEncapsulation.None
})
export class MyComponent implements OnInit {
...
}
[UPDATE] - 我忘了提到我目前正在使用 ViewEncapsulation.None 来将一些 css 文件中的样式应用到组件中。不确定这个细节是否与问题有关。
【问题讨论】:
-
您可以使用资产文件夹中的路径或cdn
document.head.appendChild(...path);将它们添加到document -
谢谢,它似乎至少适用于某些库...
标签: angular typescript angular-components angular2-components