【发布时间】:2021-06-20 11:24:01
【问题描述】:
尝试使用这个 html 模板创建一个 angular(11) 应用程序。https://bootstrapmade.com/flexstart-bootstrap-startup-template/
但在模板的 js 文件中出现此错误。
main.js:29 Uncaught TypeError: Cannot read property 'addEventListener' of null
at on (main.js:29)
at main.js:113
at main.js:317
index.html,同模板 index.html
<!-- Vendor CSS Files -->
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
<link href="assets/vendor/aos/aos.css" rel="stylesheet">
<link href="assets/vendor/remixicon/remixicon.css" rel="stylesheet">
<link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">
<link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
<!-- Template Main CSS File -->
<link href="assets/css/style.css" rel="stylesheet">
还有js,在angular.json
"scripts": [
"src/assets/vendor/bootstrap/js/bootstrap.bundle.js",
"src/assets/vendor/aos/aos.js",
"src/assets/vendor/php-email-form/validate.js",
"src/assets/vendor/swiper/swiper-bundle.min.js/",
"src/assets/vendor/purecounter/purecounter.js",
"src/assets/vendor/isotope-layout/isotope.pkgd.min.js",
"src/assets/vendor/glightbox/js/glightbox.min.js",
"src/assets/js/main.js"
]
和main.js 代码
[![(function() {
"use strict";
/**
* Easy selector helper function
*/
const select = (el, all = false) => {
el = el.trim()
if (all) {
return \[...document.querySelectorAll(el)\]
} else {
return document.querySelector(el)
}
}
/**
* Easy event listener function
*/
const on = (type, el, listener, all = false) => {
if (all) {
select(el, all).forEach(e => e.addEventListener(type, listener))
} else {
select(el, all).addEventListener(type, listener)
}
}]
【问题讨论】:
-
我猜第 18 行返回 null?那个元素
.mobile-nav-toggle是在调用它时渲染的吗? -
请分享你的html
-
你能发布一个 stackblitz 演示吗?
-
通常使用角度,您会将依赖于视图的代码(如带有 .mobile-nav-toggle 的标签)放入 ngOnInit() 以确保您的标签存在 angular.io/api/core/OnInit ...也将是很高兴发布您的 html 文件
标签: angular typeerror html-templates