WebComponents:Firefox 自定义元素未显示
WebComponents: Firefox custom-element is not showing
我正在使用网络组件 "custom elements" 功能并且需要支持旧浏览器(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);
}
WebComponentsReady 已触发,并且已在侦听器回调中调用 define/register 元素,但元素未在 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>
更多信息:
HTMLElement 只能在实现自定义元素时进行扩展(使用 polyfill 本机实现)。
因此,您必须仅在加载 polyfill 后定义 <wc-button>
自定义元素 class。
在你的例子中:
let registerElement = () => {
if(!window.customElements.get("wc-button")){
//define the WCButton class here
class WCButton extends HTMLElement {
//...
}
window.customElements.define(‘wc-button', WCButton);
}
};
我正在使用网络组件 "custom elements" 功能并且需要支持旧浏览器(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);
}
WebComponentsReady 已触发,并且已在侦听器回调中调用 define/register 元素,但元素未在 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>
更多信息:
HTMLElement 只能在实现自定义元素时进行扩展(使用 polyfill 本机实现)。
因此,您必须仅在加载 polyfill 后定义 <wc-button>
自定义元素 class。
在你的例子中:
let registerElement = () => {
if(!window.customElements.get("wc-button")){
//define the WCButton class here
class WCButton extends HTMLElement {
//...
}
window.customElements.define(‘wc-button', WCButton);
}
};