简体   繁体   English

如何将 JSON Object 字符串文字传递到 LitElement

[英]How to pass JSON Object string literal into LitElement

I am looking at if the following is viable.我正在研究以下是否可行。

My server side-code serializes data that drives a (Web) component into a JSON object, which is then renderable into the HTML the server generates as a string.我的服务器端代码将驱动(Web)组件的数据序列化为 JSON object,然后可以将其渲染到服务器生成为字符串的 HTML 中。 I want to pass this stringified JSON object into my LitElement web component and have my LitElement component automatically deserialize the string into a native JavaScript Object. I want to pass this stringified JSON object into my LitElement web component and have my LitElement component automatically deserialize the string into a native JavaScript Object.

For example:例如:

HTML from the server will generate:来自服务器的 HTML 将生成:

<my-lit-element myData="{\"hello\": \"world\"}"/>

I want my LitElement (using Typescript) to be able to do something like:我希望我的LitElement (使用 Typescript)能够执行以下操作:

@customElement('my-lit-element')
export class MyLitElement extends LitElement {
    @property({type: String) myData = '{}'

    render() {
        return html`
        <div>${this.myData.hello}</div>
    `;
    }   
}

Is this possible?这可能吗?

To answer the question why I don't just make a LitElement property for each myData key: I have a number of instances where myData's properties may contain lists of other multi-property objects, that I need to somehow pass into the LitElement , so that it can loop over them and render their contents.要回答为什么我不只是为每个 myData 键创建一个LitElement属性的问题:我有许多实例,其中 myData 的属性可能包含其他多属性对象的列表,我需要以某种方式传递给LitElement ,这样它可以循环它们并呈现它们的内容。

The answers I've found here seem to address binding JavaSCript objects within the render method and not passing IN string JS Objects (via HTML) to the LitElement .我在这里找到的答案似乎解决了在渲染方法中绑定 JavaSCript 对象,而不是将 IN 字符串 JS 对象(通过 HTML)传递给LitElement

LitElement has some default converter built for example for object it uses JSON.parse automatically LitElement 有一些默认转换器,例如为 object 它使用 JSON.parse 自动

See more details at the docs https://lit-element.polymer-project.org/guide/properties#conversion-type在文档https://lit-element.polymer-project.org/guide/properties#conversion-type中查看更多详细信息

So if you have an html like so因此,如果您有这样的 html

Note: custom elements can not be self closing according to spec注意:自定义元素不能根据规范自动关闭

Note: escaping quotes in html is not really a thing - you need to use " instead注意: html 中的 escaping 引号并不是真正的东西 - 您需要使用 " 代替

<my-lit-element myData="{&quot;hello&quot;: &quot;world&quot;}"></my-lit-element>

then all you need to convert the string to an object is to define it as an object那么将字符串转换为 object 所需要做的就是将其定义为 object

@customElement('my-lit-element')
export class MyLitElement extends LitElement {
    @property({type: Object) myData = '{}'

    render() {
        console.log(this.myData); // { hello: 'world' } 
        return html`
          <div>${this.myData.hello}</div>
        `;
    }   
}

if more custom conversions are needed you can provide your own converter https://lit-element.polymer-project.org/guide/properties#conversion-converter如果需要更多自定义转换,您可以提供自己的converter https://lit-element.polymer-project.org/guide/properties#conversion-converter

Live Demo:现场演示:

 <h1>My Demo</h1> <my-lit-element myData="{&quot;hello&quot;: &quot;world&quot;}"></my-lit-element> <script type="module"> import { LitElement, html } from 'https://unpkg.com/lit-element?module'; export class MyLitElement extends LitElement { static get properties() { return { myData: { type: Object } }; } constructor() { super(); this.myData = {}; } render() { console.log(this.myData); // { hello: 'world' } return html` <div>${this.myData.hello}</div> `; } } customElements.define('my-lit-element', MyLitElement); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM