简体   繁体   English

如何使用 LitElement 将道具从父母发送给孩子

[英]How to send props from parent to child with LitElement

Can someone show me some simple example about sending props from parent class to child?有人可以告诉我一些关于从父母 class 向孩子发送道具的简单示例吗? What is the problem:问题是什么:

parent component:父组件:

 import { LitElement, html, css } from 'lit-element'; import './child.js'; class Parent extends LitElement { constructor() { super() this.message = "hello world" } render() { return html `<child-component message=${this.message}></child-component>` //how to get this props in child component? } } customElements.define('parent-component', Parent);

and child component:和子组件:

 import { LitElement, html, css } from 'lit-element'; class Child extends LitElement {... render() { return html `<p>${message from parent, but how}</p>` //message props should go to constructor? to render method as argument? how? } } } customElements.define('child-component', Child);

ok, I found solution.好的,我找到了解决方案。 If I want to define properties in Parent class I have to add dot.如果我想在父 class 中定义属性,我必须添加点。

Reference: https://lit-element.polymer-project.org/guide/properties参考: https://lit-element.polymer-project.org/guide/properties

 render() { return html `<child-component.message=${this.message}></child-component>` }

So now everything is working.所以现在一切正常。

And full example:和完整的例子:

parent component:父组件:

 import { LitElement, html, css } from 'lit-element'; import './child.js'; class Parent extends LitElement { constructor() { super() this.message = "hello world" } render() { return html `<child-component.message=${this.message}></child-component>` } } customElements.define('parent-component', Parent);

child component:子组件:

 import { LitElement, html, css } from 'lit-element'; class Child extends LitElement {... render() { return html `<p>${this.message}</p>` //this.message have "hello world" value now } } customElements.define('child-component', Child);

Make it much standard.让它变得很标准。

In your child component., set properties to listen to message.在您的子组件中,设置属性以收听消息。

 import { LitElement, html, css } from 'lit-element'; class Child extends LitElement { static get properties(){ return{ message: { type: String, } } } constructor(){ super(); console.log(message); } render() { return html `<p>${this.message}</p>` //this.message have "hello world" value now } } customElements.define('child-component', Child);

In children to load property from parent component:在子组件中从父组件加载属性:

 @property({type: Number}) propertyChild: number; constructor() { super(); this.propertyChild = (this.parentElement as TypeParent).propertyParent; }

In parent to update properties on children:在父级中更新子级的属性:

 override updated(): void { this.childNodes.forEach(node=>{ const nodeType = node as TypeChildren; if(nodeType.property) { nodeType.propertyChild = this.propertyParent; } }) }

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

相关问题 将道具从父级发送到子级,并在子级组件(ReactJs)中对其进行更新 - Send props from parent to child and update them in child component (ReactJs) "LitElement 不会从父组件更新子组件" - LitElement doesn't update child component from parent component 如何在反应中将道具从父母传递给孩子? - How to pass props from parent to child in react? 如何通过地图将道具从父母传递给孩子 - How to pass props to child from parent with a map 如何将道具从父组件渲染到子组件? - How to render props from parent component to child? 使用道具将数据从子级发送到父级以进行反应 - send data from child to parent in react using props 从孩子向父母发送道具 | 基于 React class 的组件 - Send props to parent from child | React class based Component 如何将道具从父母传给孩子? - How to pass the props from parent to child to child of child? 如何在 React.js 中将 state 作为道具从父母传递给孩子,然后从该孩子传递给它的孩子 - How to pass state from parent to child as props then from that child to it s child as props in React.js 如何将道具从父组件添加到子组件的子组件 - How to add props from parent component to child component of child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM