如何将Typed.js与Angular 2集成?

inf*_*dev 6 javascript angular typedjs

我会使用Typedjs以角度2制作打字机效果

网站所示,我已经用npm安装了包:

npm install typed.js
Run Code Online (Sandbox Code Playgroud)

然后我将此代码添加到我的组件:

import Typed from 'typed.js';// not sure if it's the right way 
Run Code Online (Sandbox Code Playgroud)

ngOnInit() {


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

然后在我的html文件中:

<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>
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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

PS:我对任何其他与Typed.js做同样工作并与Angular 2/4合作的提议持开放态度

Lia*_*dan 8

您需要将导入更改为 import * as Typed from 'typed.js';


Vis*_*ale 8

在你的项目中安装 typed.js 包

npm install typed.js --save
Run Code Online (Sandbox Code Playgroud)

在你的 component.ts 文件中导入 typed.js

import Typed from 'typed.js';
Run Code Online (Sandbox Code Playgroud)

将此代码添加到 component.ts 的 ngOnInit() 中

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

const typed = new Typed('.typed-element', options);
Run Code Online (Sandbox Code Playgroud)

创建 html 元素以呈现 typed.js 的输出

<span class="typed-element">Fsfsfsfsfs</span>  
Run Code Online (Sandbox Code Playgroud)

请参考https://github.com/vishalhulawale/angular/tree/master/integrations/typedJS


Ted*_*rne 1

尝试将您的更改更改为从模块import导入,如下所示:Typedtyped.js

import { Typed } from 'typed.js';
Run Code Online (Sandbox Code Playgroud)