[英]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);
});
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:- 以下是您可以遵循的一些步骤,以实现所需的功能:-
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>
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.