gpt4 book ai didi

javascript - Web 组件 : Firefox custom-element is not showing

转载 作者:行者123 更新时间:2023-11-30 14:04:03 26 4
gpt4 key购买 nike

我正在使用 Web 组件“自定义元素”功能并且需要支持旧浏览器(Firefox v60),因此不是通过加载所有 polyfill 的 webcomponent-loader.js 加载 polyfill。基于特征检测的懒加载自定义元素polyfill

(function() {
if(!window.customElements){
var ce = document.createElement('script');
ce.type = 'text/javascript';
ce.async = true;
ce.src = 'https://unpkg.com/@webcomponents/custom-elements@1.2.4/custom-elements.min.js';
/**

* loading "customElement" polyfills wont't fire "WebComponentsReady" event, it will be called when we use
* "webcomponent-loader.js" but it will load other polyfills("shadow-dom"), so loading the "customElements" polyfill alone
* based on feature detection and firing "WebComponentsReady" event manually.
*/
ce.onload = function() {
document.dispatchEvent(
new CustomEvent('WebComponentsReady', {bubbles: true}));
};
var st = document.getElementsByTagName('script')[0];
st.parentNode.insertBefore(ce, st);
}
})()

并在加载时手动触发WebComponentsReady 事件。注册如下元素

let registerElement = () => {
if(!window.customElements.get(“wc-button")){
window.customElements.define(‘wc-button', WCButton);
}
};

if(window.customElements){
registerElement();
} else {
document.addEventListener('WebComponentsReady', registerElement);
}

WebComponentsReadygot 已触发并在监听器回调中被调用以定义/注册元素,但该元素未在 firefox60.6.1esr(64 位)的页面中显示或加载

最佳答案

webcomponents-loader.js 为你做特征检测而不是等待 WebComponentsReady 事件,你做

<script src="https://unpkg.com/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script>
window.WebComponents.waitFor(() => {
// do stuff that needs the polyfill
})
</script>

更多信息:

关于javascript - Web 组件 : Firefox custom-element is not showing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55795053/

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