简体   繁体   English

在Angular中集成JS的正确方法

[英]Proper way to integrate JS in Angular

I want to include a JS library in my Angular app. 我想在我的Angular应用程序中包含一个JS库。 I have searched for this and I found many approaches. 我搜索过这个,我找到了很多方法。 In my case I will include this https://mauriciopoppe.github.io/function-plot/ library. 在我的情况下,我将包括这个https://mauriciopoppe.github.io/function-plot/库。 My expectation was that I can create a module eg 'plot-function' that provides a function tag that takes the options the library needed. 我的期望是我可以创建一个模块,例如'plot-function',它提供了一个功能标签,可以获取库所需的选项。

As example: 例如:

<div class="somediv">
    <ng-function [options]="options" [function]="function"></ng-function>
</div>

But I can't find an approache that will work like this. 但我找不到像这样工作的方法。 Do you have any ideas, or is it not possible? 你有什么想法,或者不可能吗?

There are a few considerations & different answers depending on what kind of library you are trying to integrate... 根据您尝试集成的库类型,有一些注意事项和不同的答案...

  1. The library is an NPM module 该库是NPM模块

If so, import it in to your code using the most up-to-date module syntax the library supports - ideally using some kind of ES6/TypeScript import syntax covered here . 如果是这样,请使用库支持的最新模块语法将其导入到您的代码中 - 理想情况下使用此处介绍的某种ES6 / TypeScript导入语法 If the library doesn't support one of these modern import syntaxes, you can still use ES5 require syntax: 如果库不支持这些现代导入语法之一,您仍然可以使用ES5 require语法:

const myModule = require('myModule');
  1. The library is not an NPM module 该库不是NPM模块

See this official guide 请参阅此官方指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM