简体   繁体   English

如何将第3方JS库加载到我的Angular6项目中?

[英]How do I load a 3rd party JS library into my Angular6 project?

There is an awesome JS library called 't.js'. 有一个很棒的JS库,叫做“ t.js”。 It's used to make a cool looking typewriter effect. 它用于制作酷炫的打字机效果。 This library cannot be downloaded via npm , you have to download its t.min.js file. 该库无法通过npm下载,您必须下载其t.min.js文件。

Usually when adding libraries to an Angular6 project, you have to install it via npm , but as I said, you can't. 通常,将库添加到Angular6项目时,您必须通过npm安装它,但是正如我所说,您不能这样做。 So I tried pasting a script tag in my html file to reference it. 因此,我尝试将script标签粘贴到html文件中以进行引用。 But, now it keeps giving me an error: 但是,现在它一直给我一个错误:

$(...).t is not a function

You can check out t.js' website here . 您可以在此处查看t.js的网站。 You will then see their documentation and understand the use of the .t I used. 然后,您将看到他们的文档,并了解我使用的.t用法。

So does anyone maybe know how I can add a 3rd party JS library to my Angular6 project when it can't be installed via npm ? 那么,有没有人知道我无法通过npm安装第三方JS库到我的Angular6项目吗?

After some Google-ing some people said the error is that the library is .js and not .ts , if this helps. 经过一番Google搜索之后,有人说错误是该库是.js而不是.ts ,如果有帮助的话。

Here is also some of the code I used: 这也是我使用的一些代码:

<script src="assets/t.min.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
  setTimeout(() => {
    $('#main').t({
      beep: true,
      caret: '<span style="color: hotpink;">·</span>',
      typing: (elm, chr) => {
        if (chr.match(/\-trigger/)) {
          $('#pow-txt').show().delay(500).fadeOut(0);
        }
      }
    });
  }, 4200);
});

I would also recommend this video . 我也推荐这部影片

You should not use jquery in angular it will cause so many problems later. 您不应该在angular中使用jquery,这会在以后引起很多问题。 But if you want to do this cool stuff in angular then you can easily do that by adding a script tag in index.html file. 但是,如果您想做一些有趣的事情,可以通过在index.html文件中添加脚本标签来轻松实现。

Following are some steps that you can follow to achieve what you need:- 以下是您可以遵循的一些步骤,以实现所需的功能:-

  1. Add assets folder and put your jquery.js and t.js file. 添加资产文件夹,然后放入jquery.js和t.js文件。
  2. Link these script in index.html . 将这些脚本链接到index.html ex
      <script src="./assets/js/jquery.min.js" type="text/javascript"></script> <script src="./assets/js/t.min.js" type="text/javascript"></script> 
  3. In your component ts file you have to declare jQuery variable to link it with global jQuery variable. 在您的组件ts文件中,您必须声明jQuery变量以将其与全局jQuery变量链接。 Use this variable to do all jquery operation. 使用此变量执行所有jquery操作。

In component ts file 在组件ts文件中

import { Component } from '@angular/core';
declare var jQuery;
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';

  runDemo1() {
    jQuery('#demo_1').t({
      beep: true,
      caret: '<span style="color:hotpink;">•</span>',
      typing: function (elm, chr) {
        if (chr.match(/\-trigger/))
          jQuery('#pow-txt').show().delay(500).fadeOut(0);
      }
    });
  }
}

In component html file 在组件html文件中

<button (click)="runDemo1()">Run demo</button>

<pre id="demo_1">
######<ins><span id="pow-txt" style="display:none;color:tomato;">---[POW!]</span></ins>
##<del>\<ins>1.5</ins></del><del id="pow-trigger">|</del>\
</pre>

you can follow this stackblitz 您可以按照此堆叠闪电战

That looks like an error that'd be thrown if jQuery isn't installed & loaded. 如果未安装和加载jQuery,这似乎会引发错误。 As an alternative, you could try TypeIt, an animated typing utility I wrote. 或者,您可以尝试TypeIt,这是我编写的动画打字工具。 It has no jQuery dependency, and can be easily installed with npm : npm install typeit . 它没有jQuery依赖性,可以通过npm轻松安装: npm install typeit

Site/Docs: https://typeitjs.com/ 网站/文档: https : //typeitjs.com/

Github: https://github.com/alexmacarthur/typeit GitHub: https : //github.com/alexmacarthur/typeit

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

相关问题 如何使用emscripten调用第三方JavaScript库? - How do I call a 3rd party javascript library with emscripten? 我该如何调用组件内部的方法,该方法是从react.js中的第3方库更新的? - How do I call a method inside component did update from a 3rd party library in react.js? 如何从第三方库访问角度分量 - How to access angular component from 3rd party library 如何在 SAPUI5 中使用第三方 JS 库 - How to use 3rd party JS library with SAPUI5 流星ReactJs和第三方js库 - Meteor ReactJs and 3rd party js library 在webpack中包含第三方JS库 - Include 3rd party JS library in webpack 如何将第 3 方 javascript 模块“导入”到我的反应项目中? - How to “import” a 3rd party javascript module to my react project? 如何强制我的 web 应用程序仅在任何第 3 方应用程序 Web 视图之外加载? - How can I Force my web application to load only outside of any 3rd party app webviews? 如何收集我的项目中使用第 3 方依赖项的次数及其方法? - How can I collect the amount of times a 3rd party dependency is used in my project and it's methods? 我如何在 angular 中使用 3rd 方 jquery 插件? - how can i use 3rd party jquery plugins in angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM