简体   繁体   中英

Add JavaScript library/code in Angular CLI

I am trying to use the Typed.js library in Angular CLI, but I couldn't manage to make it work. I followed the instructions in How can I use JavaScript code in Angular 7? : copied the jQuery library and typed scrips in assets folder, added a path to the files in angular.json, but I am not sure what to do in the component.ts file:

This is my component.ts code:

import { Component, OnInit } from '@angular/core';
declare var jQuery: any;
declare var Typed: any;

@Component({
  selector: 'app-typing',
  templateUrl: './typing.component.html',
  styleUrls: ['./typing.component.scss']
})
export class TypingComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    var typed = new Typed('.typed', {
      strings: ["First sentence.", "Second sentence."],
      typeSpeed: 30
    });
  }

  onComplete() {
  }

}

I am pretty sure what I wrote in ngOnInit() is wrong, but how can I make it right?

You have to import typed.js in order to be able to use it. Declaring it as variable with type of any does not make any sense. Try importing it instead like this: import Typed from 'typed.js';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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