Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Typed.js with Angular 2?

I would make a Typewriter effect in angular 2 using Typedjs

As indicated the site page, I have installed the package with npm :

npm install typed.js

Then I have added this code to my component :

import Typed from 'typed.js';// not sure if it's the right way 

and

ngOnInit() {


    var typed = new Typed(".element", {
        strings: ["This is a JavaScript library", "This is an ES6 module"],
        smartBackspace: true // Default value
    });
}

Then in my html file :

<div id="typed-strings">
    <p>Typed.js is an <strong>Awesome</strong> library.</p>
    <p>It <em>types</em> out sentences.</p>
</div>
<span id="typed"></span>

I get this error :

/home/haddad/projects/siteperso/src/app/slide-presentation/slide-presentation.component.ts (18,21): Cannot find name 'Typed'.

PS: i'm open to any other proposition that do the same job as Typed.js and work with Angular 2/4

like image 465
infodev Avatar asked Jul 13 '17 15:07

infodev


Video Answer


2 Answers

Install typed.js package in your project

npm install typed.js --save

Import typed.js in your component.ts file

import Typed from 'typed.js';

Add this code in ngOnInit() of component.ts

const options = {
     strings: ['Innovation.', 'Discovery.'],
     typeSpeed: 100,
     backSpeed: 100,
     showCursor: true,
     cursorChar: '|',
     loop: true
};

const typed = new Typed('.typed-element', options);

Create html element to render output of typed.js

<span class="typed-element">Fsfsfsfsfs</span>  

Please refer to https://github.com/vishalhulawale/angular/tree/master/integrations/typedJS

like image 171
Vishal Hulawale Avatar answered Oct 15 '22 19:10

Vishal Hulawale


You need to change your import to import * as Typed from 'typed.js';

like image 37
Liad Idan Avatar answered Oct 15 '22 19:10

Liad Idan