我对我正在处理的特定元素有疑问。由于某种原因,这个不允许我在 html 标记上指定任何属性值。
如果我在构造函数中指定默认值,那么该元素可以与这些默认值一起正常工作,但我无法通过声明性 html 覆盖默认值。
这是元素:
class OstinatoFetchTriggers extends LitElement {
static get properties() {
return {
/**
* The query selector for the `ostinato-fetch` element to use when
* making the request.
*/
xhrSelector: { type: String },
/**
* Elements with the trigger selector will have their click event
* intercepted and will make the request via ostinato-fetch
*/
triggerSelector: { type: String },
};
}
constructor() {
super();
this.xhrSelector = '#xhrContent';
this.triggerSelector = '[xhr-link]';
}
connectedCallback() {
super.connectedCallback();
// The output for console log below is null or whatever the default was in the contructor.
console.log(this.triggerSelector);
var triggerList = document.querySelectorAll(this.triggerSelector);
triggerList.forEach((trigger) => {
trigger.addEventListener('click', this._handleXhrClick.bind(this));
});
}
disconnectedCallback() {
super.disconnectedCallback();
var triggerList = document.querySelectorAll(this.triggerSelector);
triggerList.forEach((trigger) => {
trigger.removeEventListener('click', this._handleXhrClick.bind(this));
});
}
_handleXhrClick(ev) {
ev.preventDefault();
this.triggerRequest(ev.currentTarget.href);
}
triggerRequest(href) {
document.querySelector(this.xhrSelector).fetch(href);
}
}
customElements.define('ostinato-fetch-triggers', OstinatoFetchTriggers);
我尝试像这样使用上面的元素:<ostinato-fetch-triggers xhr-selector="#somethingElse"></ostinato-fetch-triggers>
.
我期望元素中的 xhrTriggers 属性应该是#somethingElse
,但事实并非如此。它基本上只是使用构造函数的默认值。