我在運行到ES5時遇到了一個簡單的Web組件工作問題。它在chrome,Edge和Firefox下似乎運行得很好,但IE11在組件的構造函數中失敗,「自定義元素構造函數未生成正在升級的元素」。從Typescript轉換到ECMAScript 5在IE11下失敗的自定義元素v1
UPDATE
奧利弗克魯爾的出色工作下面已經明確牽制的問題打字稿的編譯器輸出。是否有可能使其工作?
原始源(打字稿):
import "./AppDrawer.less"
class AppDrawer extends HTMLElement {
get open() {
return this.hasAttribute("open");
}
set open(val: boolean) {
val ? this.setAttribute("open", '') : this.removeAttribute('open');
}
get disabled() {
return this.hasAttribute("disabled");
}
set disabled(val: boolean) {
val ? this.setAttribute("disabled", '') : this.removeAttribute('disabled');
}
static get observedAttributes() { return ["open"] };
constructor() {
super();
}
connectedCallback() {
this.addEventListener("click",() => {
this.open = !this.open;
})
this.textContent = this.open ? "OPEN": "CLOSED";
}
attributeChangedCallback(attr, oldVal, newVal) {
this.textContent = this.open ? "OPEN": "CLOSED";
}
}
customElements.define("app-drawer", AppDrawer)
輸出(bundle.js
):
(function() {
'use strict';
function __$styleInject(css) {
if (!css) return;
if (typeof window == 'undefined') return;
var style = document.createElement('style');
style.setAttribute('media', 'screen');
style.innerHTML = css;
document.head.appendChild(style);
return css;
}
__$styleInject("app-drawer {\n color: red;\n}\n");
function __extends(d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var AppDrawer = (function (_super) {
__extends(AppDrawer, _super);
function AppDrawer() {
_super.call(this);
}
Object.defineProperty(AppDrawer.prototype, "open", {
get: function() {
return this.hasAttribute("open");
},
set: function (val) {
val ? this.setAttribute("open", '') : this.removeAttribute('open');
},
enumerable: true,
configurable: true
});
Object.defineProperty(AppDrawer.prototype, "disabled", {
get: function() {
return this.hasAttribute("disabled");
},
set: function (val) {
val ? this.setAttribute("disabled", '') : this.removeAttribute('disabled');
},
enumerable: true,
configurable: true
});
Object.defineProperty(AppDrawer, "observedAttributes", {
get: function() { return ["open"]; },
enumerable: true,
configurable: true
});
AppDrawer.prototype.connectedCallback = function() {
var _this = this;
this.addEventListener("click", function() {
_this.open = !_this.open;
});
this.textContent = this.open ? "OPEN" : "CLOSED";
};
AppDrawer.prototype.attributeChangedCallback = function (attr, oldVal, newVal) {
this.textContent = this.open ? "OPEN" : "CLOSED";
};
return AppDrawer;
}(HTMLElement));
customElements.define("app-drawer", AppDrawer);
}());
而我的HTML:
<!doctype html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.0-rc.8/webcomponents-lite.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.0-rc.8/custom-elements-es5-adapter.js"></script>
<script src="bundle.js"></script>
</head>
<body>
<app-drawer open disabled></app-drawer>
</body>
</html>
@ mseddon我的答案幫助你了嗎? –
好的,我已經嘗試了native-shim(在bundle.js之前添加),並觀察它現在在Edge,FF和Chrome中運行!但IE11仍然提出了相同的錯誤,其餘的我可以編譯成ES6。有沒有辦法讓它在包括IE11在內的所有平臺上工作? – mseddon
噢,我也必須刪除'custom-elements-es5-adapter.js'才能在Chrome下正常工作。 – mseddon