小编tug*_*end的帖子

Lit-elements,编写受控组件的惯用方式

我正在通过@open-wc使用lit-elements,目前正在尝试编写一组嵌套的组件,其中内部组件是一个输入字段,并且某些祖先组件必须支持一些任意重写规则,例如“不允许输入数字” '。

我想弄清楚的是使用 lit-elements 构建它的正确方法什么。在React 中,我会使用一个“受控组件”,在这里可以轻松地强制所有组件提交给根组件属性。

下面的示例是我使用 Lit-Elements 提出的。有没有更好的方法来做到这一点

请注意; 因为我想忽略一些字符,所以挑战变得稍微困难​​一些。如果没有e.target.value = this.value;at 级别 5,输入 elmement 将与忽略字符上的组件状态不同。我希望整个组件链正确同步,因此以标头标签为例。

export class Level1 extends LitElement {
  static get properties() {
    return {
      value: { type: String }
    };
  }

  render() {
    return html`
      <div>
        <h1>${this.value}</h1>
        <level-2 value=${this.value} @input-changed=${this.onInput}></level-2>
      </div>`;
  }

  onInput(e) {
    this.value = e.detail.value.replace(/\d/g, ''); 
  }
}

... …
Run Code Online (Sandbox Code Playgroud)

javascript web-component lit-element controlled-component

5
推荐指数
1
解决办法
643
查看次数

为什么我需要将IDictionary <T,HashSet <S >>转换为IDictionary <T,IEnumerable <S >>?

以下代码自HashSet实现以来有效IEnumerable:

IEnumerable<TEdge> edges = new HashSet<TEdge>();
Run Code Online (Sandbox Code Playgroud)

但是如果我尝试在Dictionary中使用与键入值相同的值,则会出现编译错误:

IDictionary<TVertex, IEnumerable<TEdge>> vertexEdges = 
    new Dictionary<TVertex, HashSet<TEdge>>();  
Run Code Online (Sandbox Code Playgroud)

Cannot implicitly convert type 'System.Collections.Generic.Dictionary<TVertex,System.Collections.Generic.HashSet<TEdge>>' to 'System.Collections.Generic.IDictionary<TVertex,System.Collections.Generic.IEnumerable<TEdge>>'. An explicit conversion exists (are you missing a cast?)

我在这里错过了什么?当然编译器应该能够弄清楚这一点,所以我猜测要么必须在限制背后做出一些有意义的决定,要么我做错了.

c# dictionary casting type-inference

2
推荐指数
1
解决办法
145
查看次数