javascript - 错误类型错误 : Cannot read property 'tagNam

我会使用 TypedJS 库。

我已经使用 npm install typed.js 安装了它 然后在 .angular-cli.json 中包含 typed.js 文件

"scripts": [
  "../node_modules/jquery/dist/jquery.min.js",
  "../node_modules/typed.js/lib/typed.js"
],

在我的组件中:

import { Component, OnInit } from '@angular/core';
import * as Typed from 'typed.js';

@Component({
  selector: 'app-a-propos',
  templateUrl: './a-propos.component.html',
  styleUrls: ['./a-propos.component.css']
})
export class AProposComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    var typed2 = new Typed('#typed', {
      strings: ["<b>#Font End ?</b>", "<b>#Front End ?</b>", "<b>#Back End ?</b>", "<b> #Fullstack ?</b>", "<b>ça m'interesse !</b>"],
      typeSpeed: 30,
      backDelay: 2000,
      loop: true,

    });
  }
}

和 HTML

<p id="typed"> </p>

我收到这个错误:

ERROR TypeError: Cannot read property 'tagName' of null

最佳答案

您需要在 View 初始化后设置 Typed 对象。

export class AProposComponent implements AfterViewInit{

  constructor() { }

  ngAfterViewInit() {
    var typed2 = new Typed('#typed', {
      strings: ["<b>#Font End ?</b>", "<b>#Front End ?</b>", "<b>#Back End ?</b>", "<b> #Fullstack ?</b>", "<b>ça m'interesse !</b>"],
      typeSpeed: 30,
      backDelay: 2000,
      loop: true,

    });
  }
}

关于javascript - 错误类型错误 : Cannot read property 'tagName' of null when using Typedjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225019/

相关文章:

security - 自动关联社交登录有什么安全风险(Oauth2)

python - BeautifulSoup- find_all- 订单保存

google-bigquery - bigquery 备份所有 View 定义

android - 获取屏幕曲线、角和切口的物理形状

java - 运行时使用插件的不同库版本

apache-flink - 在不重启作业的情况下在 Apache Flink 中动态添加模式

python - 删除使用 imageio 或 PIL 创建的 GIF 中的空间插值或压缩

c# - 如何在 OData v4 中获取枚举值而不是枚举名称?

json - 使用 REST API 的 POST 方法进行读取操作

python - 模仿 AWS CLI/gcloud 命令行参数