How to integrate Typed.js with Angular 2?

6.3k views Asked by At

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

5

There are 5 answers

0
Teddy Sterne On BEST ANSWER

Try changing your import to import Typed from the typed.js module like so:

import { Typed } from 'typed.js';
0
Raul Martin On

In order to say "new Typed" you need a constructor, and for that you need to import like this the Library:

import Typed from 'typed.js/src/typed.js';
1
Vishal Hulawale On

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>  
0
Liad Idan On

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

0
FelipeHSouza On

There is a package in NPM that you can use. And it is quite simple

PACKAGE URL AND DOCUMENTATION
https://www.npmjs.com/package/ngx-typed-js

COMMAND

npm install --save ngx-typed-js