自定义元素 class:this.getAttribute('data-*') returns 空
Custom Element class: this.getAttribute('data-*') returns null
我已经复制并粘贴到 Mozzila 示例中的代码
https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements#Observed_attributes
到我电脑上的文件,当我 运行 它时,我从每次调用 this.getAttribute 得到空值。
我看到它在上面的 link 上工作,但是当我 运行 我复制的项目时,它是空的,基于这个例子,我写的另一个项目也发生了同样的事情:
HTML 文件:
If nothing appeared below, then your browser does not support Custom Elements yet.
<x-product data-name="Ruby" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/ruby.png" data-url="http://example.com/1"></x-product>
<x-product data-name="JavaScript" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/javascript.png" data-url="http://example.com/2"></x-product>
<x-product data-name="Python" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/python.png" data-url="http://example.com/3"></x-product>
JS 文件:
// Create a class for the element
class XProduct extends HTMLElement {
constructor() {
// Always call super first in constructor
super();
// Create a shadow root
var shadow = this.attachShadow({mode: 'open'});
// Create a standard img element and set it's attributes.
var img = document.createElement('img');
img.alt = this.getAttribute('data-name');
img.src = this.getAttribute('data-img');
img.width = '150';
img.height = '150';
img.className = 'product-img';
// Add the image to the shadow root.
shadow.appendChild(img);
// Add an event listener to the image.
img.addEventListener('click', () => {
window.location = this.getAttribute('data-url');
});
// Create a link to the product.
var link = document.createElement('a');
link.innerText = this.getAttribute('data-name');
link.href = this.getAttribute('data-url');
link.className = 'product-name';
// Add the link to the shadow root.
shadow.appendChild(link);
}
}
// Define the new element
customElements.define('x-product', XProduct);
您应该在 connectedCallback()
方法中使用 this.getAttribute()
,因为调用 constructor()
方法时属性可能尚未定义。
这里的情况是 constructor()
在 <x-product>
被解析后立即被调用,此时它的属性还没有附加。
请注意,如果您将 customElement.define()
语句 放在 html 代码 之后,它仍然可以工作 <x-product data-...>
。这是因为当标签被定义为自定义元素时,属性已经附加到 <x-product>
元素。
查看 了解更多详情。
我还 运行 解决了从构造函数中调用 getAttribute 时返回 null 的问题。我注意到在示例 index file 中,标签具有 defer 属性。一旦我将 defer 属性添加到我的脚本标记中,构造函数中的 getAttribute 调用就开始工作了。
我已经复制并粘贴到 Mozzila 示例中的代码 https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements#Observed_attributes 到我电脑上的文件,当我 运行 它时,我从每次调用 this.getAttribute 得到空值。 我看到它在上面的 link 上工作,但是当我 运行 我复制的项目时,它是空的,基于这个例子,我写的另一个项目也发生了同样的事情:
HTML 文件:
If nothing appeared below, then your browser does not support Custom Elements yet.
<x-product data-name="Ruby" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/ruby.png" data-url="http://example.com/1"></x-product>
<x-product data-name="JavaScript" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/javascript.png" data-url="http://example.com/2"></x-product>
<x-product data-name="Python" data-img="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4621/python.png" data-url="http://example.com/3"></x-product>
JS 文件:
// Create a class for the element
class XProduct extends HTMLElement {
constructor() {
// Always call super first in constructor
super();
// Create a shadow root
var shadow = this.attachShadow({mode: 'open'});
// Create a standard img element and set it's attributes.
var img = document.createElement('img');
img.alt = this.getAttribute('data-name');
img.src = this.getAttribute('data-img');
img.width = '150';
img.height = '150';
img.className = 'product-img';
// Add the image to the shadow root.
shadow.appendChild(img);
// Add an event listener to the image.
img.addEventListener('click', () => {
window.location = this.getAttribute('data-url');
});
// Create a link to the product.
var link = document.createElement('a');
link.innerText = this.getAttribute('data-name');
link.href = this.getAttribute('data-url');
link.className = 'product-name';
// Add the link to the shadow root.
shadow.appendChild(link);
}
}
// Define the new element
customElements.define('x-product', XProduct);
您应该在 connectedCallback()
方法中使用 this.getAttribute()
,因为调用 constructor()
方法时属性可能尚未定义。
这里的情况是 constructor()
在 <x-product>
被解析后立即被调用,此时它的属性还没有附加。
请注意,如果您将 customElement.define()
语句 放在 html 代码 之后,它仍然可以工作 <x-product data-...>
。这是因为当标签被定义为自定义元素时,属性已经附加到 <x-product>
元素。
查看
我还 运行 解决了从构造函数中调用 getAttribute 时返回 null 的问题。我注意到在示例 index file 中,标签具有 defer 属性。一旦我将 defer 属性添加到我的脚本标记中,构造函数中的 getAttribute 调用就开始工作了。