小编Hus*_*hid的帖子

如何将对象(或关联数组)作为属性值传递给我的 Web 组件

我正在制作 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" };控制台

html javascript object web-component

6
推荐指数
2
解决办法
1234
查看次数

标签 统计

html ×1

javascript ×1

object ×1

web-component ×1