gpt4 book ai didi

javascript - 使用 Polyfill 附加的 ShadowRoot 不可查询

转载 作者:行者123 更新时间:2023-11-30 14:56:19 25 4
gpt4 key购买 nike

在下面的示例中,我尝试创建一个菜单组件来试验组件层次结构。

index.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Sample Menu App</title>
<script src="js/webcomponents-lite.js"></script>
<!-- Components -->
<link rel="import" href="components/global/site-navigation.html">
</head>

<body>
<site-navigation></site-navigation>
</body>

</html>

/components/global/site-navigation.html

<link rel="import" href="nav-item.html">
<template>
<div class="nav">Header Goes here</div>
<ul class="nav">
<nav-item>Item 1</nav-item> <!-- This is a child component-->
</ul>
</template>
<script>
(function (currentDocument) {
customElements.define('site-navigation', class SiteNavigation extends HTMLElement {
constructor() {
super();
const shadowTemplate = currentDocument.querySelector('template').content.cloneNode(true);
this.DOM = this.attachShadow({ mode: 'open' });
this.DOM.appendChild(shadowTemplate);
console.log(this.DOM);
}

connectedCallback(){
this.Initialize();
}

Initialize(){
this.DOM.querySelector("div.nav").innerHTML = "Title"
}
});
})((document.currentScript || document._currentScript).ownerDocument);
</script>

/components/global/nav-item.html

<template>
<li class="nitem">
<a href="#">Elements</a>
</li>
</template>
<script>
(function(currentDocument) {
customElements.define('nav-item', class SiteNavigationItem extends HTMLElement {
constructor() {
super();
const shadowTemplate = currentDocument.querySelector('template').content.cloneNode(true);
this.DOM = this.attachShadow({ mode: 'open' });
this.DOM.appendChild(shadowTemplate);
}

connectedCallback(){
this.Initialize();
}

Initialize(){
let aTag = this.DOM.querySelector('a');
aTag.innerHTML = "Link 1"
}
});
})((document._currentScript||document.currentScript).ownerDocument);
</script>

它在 Chrome 中运行良好。我有 applied Polyfill使其在其他浏览器中工作。但是,Initialize 方法在 FireFox 中失败并显示消息 TypeError: this.DOM.querySelector(...) is null。调试时发现 this.DOM = this.attachShadow({ mode: 'open' }); 在 FF 和 Chrome 中返回不同类型的对象,在 FF 结果中,没有 querySelector !我该如何解决这个问题?

最终结果

Firefox result

Chrome 结果

Chrome Result

更新:如果删除对子组件 (nav-item) 的链接/引用,则父组件(站点导航)工作正常。

最佳答案

正如您所说,HTML Imports polyfill 似乎不支持组件层次结构。

document.currentScript也不起作用。 polyfill 将复制 <template>在主文档中 2 导入的文档。

这就是为什么当您在邮件文档中查询 querySelector( 'template' ) 时,返回的是导航项的模板,没有 div.nav里面。

作为解决方法,您在查询 <template> 时应该更加具体.

site-navigtion.html中:

<template id="site-navigation">
...
</template>
...
const shadowTemplate = currentDocument.querySelector('template#site-navigation').content.cloneNode(true);

因此您将获得正确的模板,即使在 Firefox 中也是如此。

注意: document._currentScript似乎不再适用于 Polyfill v1。

关于javascript - 使用 Polyfill 附加的 ShadowRoot 不可查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47233481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com