Angular:无法读取未定义

时间:2018-04-02 17:37:38

标签: javascript angular

我有一个从我的Appcomponent中的节点模块渲染的第三方组件。我想在第三方组件中显示一个段落。

app.component.html

<div id="dashboard" class="content-fluid aui standalone">
  <cs-sidebar [nav]="navigation" class="candisableNav">
  </cs-sidebar>
</div> 

组件

ngAfterViewInit() {
    console.log(this.rd); 
    console.log(this.el); 
    let elem = document.getElementsByClassName('sidebar-header__title')
    console.log(elem)
    let a = document.createElement('p');
    console.log(a)
    a.innerHTML = 'Version 0.5';
    console.log(elem.item(0))
    elem.item(1).appendChild(a);
}

在AppComponent的ngAfterViewInit()中,我正在尝试将一个p标签附加到CsSideBarcomponent中。 elem = document.getElementsByClassName('sidebar-header__title')正在处理div标签,我可以在控制台中看到。但我得到“无法读取属性'appendChild'的未定义”错误,因为elem.item(1).appendChild(a)。我正在推动索引1,因为我已经需要第0个索引的元素。我看到这里的帖子,我也是这样做的。请帮忙

0 个答案:

没有答案