[英]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.