Я создал небольшой веб-компонент без каких-либо фреймворков. Это мой index.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<script src="clock.js" async></script>
</head>
<body>
<clock-digital></clock-digital>
<script>
console.log(document.querySelector('clock-digital').cID);
document.querySelector('clock-digital').method();
</script>
</body>
</html>
а это мой clock.js
:
customElements.define('clock-digital', class extends HTMLElement {
get cID() {}
set cID(value) {}
constructor() {
super();
var shadowRoot = this.attachShadow({
mode: 'open'
});
var that = shadowRoot;
this.cID = setInterval(function () {
var currentdate = new Date();
that.innerHTML = `<div style="display: inline; background-color: whitesmoke; font-style: italic;">${currentdate.getHours()}:${currentdate.getMinutes()}:${currentdate.getSeconds()}</div>`;
}, 500);
}
method() {
console.log('method');
}
});
Консоль браузера показывает эту ошибку:
undefined (index):14 Uncaught TypeError: document.querySelector(...).method is not a function at (index):14
Почему мой встроенный скрипт не может получить доступ к cID
и method()
?
var clock = document.querySelector('clock-digital'); clock.method();
- person darryn.ten   schedule 28.12.2016