简体   繁体   English

当我将 javascript 文件导入 angular 时,我发现 Uncaught TypeError: Cannot read properties of null (reading 'querySelector')

[英]when i import javascript files to angular i found Uncaught TypeError: Cannot read properties of null (reading 'querySelector')

**the js files doesnt see the component element**

the error occured in this js function the (showMenu,perspective,cont,wrapper) return null And I get the following error when I launch my app这个 js 函数中发生的错误 (showMenu,perspective,cont,wrapper) 返回 null 当我启动我的应用程序时出现以下错误

function init() {
        var showMenu = document.getElementById( 'showMenu' ),
            perspectiveWrapper = document.getElementById( 'perspective' ),
            cont = perspectiveWrapper.querySelector( '.cont' ),
            contentWrapper = cont.querySelector( '.wrapper' );
            

       
    }

    init();

Here is the index.html这是 index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Assignment1</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body >
      <app-root></app-root>
 <!-- javascript libraries -->
    <script src="/assets/js/jquery-3.2.1.js"></script>
    <script src="/assets/js/bootstrap.min.js"></script>
    <script src="/assets/js/jquery.appear.min.js"></script>
    <!-- push nav -->
    <script src="/assets/js/push_nav.js"></script>
</body>
   
   
</html>

Here is my component.ts and i get an ERROR ReferenceError: Init is not defined at HomeComponent.ngAfterViewInit这是我的 component.ts,我得到一个错误 ReferenceError: Init is not defined at HomeComponent.ngAfterViewInit

import { Component, OnInit,ViewEncapsulation  } from '@angular/core';
declare function init():any;
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
  encapsulation: ViewEncapsulation.None,

})
export class HomeComponent implements OnInit {

  constructor() { 
    
  }

  ngOnInit(): void {
  }
  ngAfterViewInit():void{
    init();
  }

}

You sure about the method name you're calling您确定要调用的方法名称

Init()在里面()

or或者

init()在里面()

in component.ts write this to link js file with component.html在 component.ts 中写这个来链接 js 文件和 component.html

myScriptElement: HTMLScriptElement ;
this.myScriptElement = document.createElement("script");
    this.myScriptElement.src ="/assets/js/push_nav.js";
    document.body.appendChild(this.myScriptElement);

暂无
暂无

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

相关问题 角度测试未捕获错误:未捕获(承诺):类型错误:无法读取 null 的属性(读取“参数”) - Angular Testing Uncaught Error: Uncaught (in promise): TypeError: Cannot read properties of null (reading 'params') TypeError:无法读取 null 的属性(读取“scrollHeight”)| Angular 11 - TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11 Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”) - Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue') angular 错误类型错误:无法读取 null 的属性(读取“_rawValidators”) - angular ERROR TypeError: Cannot read properties of null (reading '_rawValidators') Angular 项目产生 javascript 错误“core.js:23018 Uncaught TypeError: Cannot read properties of undefined (reading 'id')” - Angular project produces javascript error "core.js:23018 Uncaught TypeError: Cannot read properties of undefined (reading 'id')" TypeError:无法读取 null 的属性(读取“_rawValidators”) - TypeError: Cannot read properties of null (reading '_rawValidators') 类型错误:无法读取 null 的属性(读取“数量”) - TypeError: Cannot read properties of null (reading 'quantity') TypeError:无法读取 null 的属性(读取“writeValue”) - TypeError: Cannot read properties of null (reading 'writeValue') 类型错误:无法读取 null 的属性(读取“文件名”) - TypeError: Cannot read properties of null (reading 'fileName') 我对 angular formarray TypeError 有疑问:无法读取未定义的属性(读取“0”) - i have problem with angular formarray TypeError: Cannot read properties of undefined (reading '0')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM