我正在制作 vanilla-js 网络组件,需要将对象作为其值传递给属性,但每次都将其作为字符串。
我能够将字符串传递给组件的属性,但我知道我需要传递一个对象。
var obj = { name: "John", age: 30, city: "New York" };
const template = document.createElement('template');
class TabelComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(template.content.cloneNode(true));
}
connectedCallback() {
console.log(this.content);
}
get content() {
return this.getAttribute("content")
}
}
window.customElements.define('table-component', TabelComponent);Run Code Online (Sandbox Code Playgroud)
<table-component content=obj></table-component>Run Code Online (Sandbox Code Playgroud)
我的属性“内容”应该将 obj 作为对象而不是字符串。我应该进入
{ name: "John", age: 30, city: "New York" };控制台