简体   繁体   English

typescript子组件document.getElementById返回null

[英]typescript child component document.getElementById return null

I need add "CubeView" from elmarquez/threejs-viewcube to my project. 我需要将elmarquez / threejs-viewcube中的“CubeView”添加到我的项目中。 I created component. 我创建了组件。 A component code abow: 组件代码abow:

import { Component, OnInit, Input, Output, EventEmitter  } from '@angular/core';
import { DOCUMENT } from '@angular/common';

declare let THREE: any;

@Component({
  selector: 'app-cube-view-web-gl',
  template: '<div id="viewcube"> </div>',
  styleUrls: ['./cube-view-web-gl.component.scss']
})

export class CubeViewWebGlComponent implements OnInit {

  private domElement: any;
  private renderer: any;
  private scene : any;

  ngOnInit() {
  } 

constructor() {
    this.domElement = document.getElementById('viewcube'); 
    console.log(myElement);
}
}

Browser log is writed only null. 浏览器日志仅写入null。

I tried to modify the code: 我试图修改代码:

1) 1)

const myElement: HTMLElement | null = document.getElementById('viewcube');

2) 2)

constructor(@Inject(DOCUMENT) private document: Document){
}

But the result remained the same. 但结果保持不变。

Use AfterViewInit instead of constructor something like 使用AfterViewInit代替构造函数之类的东西

export class CubeViewWebGlComponent implements OnInit,AfterViewInit  {
      ngAfterViewInit() {
        //get your element here
      }
....

you can use inside a Oninit or if you want inside the constructor you should use set time out like this 您可以在Oninit中使用,或者如果您想在构造函数中使用,则应该像这样使用set time out

///for constructor
 Ele:any;
    constructor(){
    setTimeout(()=>{
   this.Ele=document.getElementById('test');
   console.log(this.Ele);
   },100)
   }
////for OnInit

    ngOnInit(){
       this.Ele=document.getElementById('test');
       console.log(this.Ele);
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿中是否有“document.getElementById(&#39;first&#39;)”的替代方案? - Is there any alternative of `document.getElementById('first')` in typescript? angular4 / typescript中的document.getElementById替换? - document.getElementById replacement in angular4 / typescript? 打字稿中的 document.getElementById(s).document.getElementsByClassName 错误 - document.getElementById(s).document.getElementsByClassName error in typescript Angular 12 对象在 document.getElementById(elementId) 中可能为“null” - Angular 12 Object is possibly 'null' in document.getElementById(elementId) document.getelementbyid 始终在 angular 13 中返回 null - document.getelementbyid always returns null in angular 13 Angular document.getElementById 不能动态使用插值 - Angular document.getElementById not working with interpolation dynamically 替换 Angular 中的 document.getElementById('checkBox') - Replacing document.getElementById('checkBox') in Angular 如何通过 document.getElementById inAngular 更改显示 - How to change display by document.getElementById inAngular 未调用的函数添加了document.getElementById(&#39;someId&#39;)。appendChild() - Function not calling added with document.getElementById('someId').appendChild() 带有 Nrwl/Nx 的 Angular SSR - 外部库调用 document.getElementById - Angular SSR with Nrwl/Nx - external library calls document.getElementById
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM