代码之家  ›  专栏  ›  技术社区  ›  Joe

@bindable的操作顺序

  •  1
  • Joe  · 技术社区  · 7 年前

    如果我的子控件(本例中为孙辈控件)如下所示:

    <my-control region-id.two-way="location.regionId"></my-control>
    

    我什么时候可以期待 regionId 有空吗?这些文档听起来像是绑定,然后又附加了,所以不应该这样

    bind attached regionIdChanged 看看控制台,我明白了

    bind
    attached
    regionIdChange to null from undefined (default value in a dropdown)
    regionIdChange to 1 from null (1 is the actual value)
    

    我早就料到了 regionId 1岁之前 附属的

    1 回复  |  直到 7 年前
        1
  •  3
  •   Jesse Hosain Ahmed    7 年前

    变量应填入 bind()

    如果将日志记录添加到每个阶段:

    export class Child {
      @bindable()
      public field: string;
    
      constructor() {
        console.info("constructor: ", this.field);
      }
    
      bind() {
        console.info("bind: ", this.field);
      }
    
      attached() {
        console.info("attached: ", this.field);
      }
    }
    

    它生成以下日志输出:

    constructor:  undefined
    bind:  test
    attached:  test
    

    哪里 test 是我绑定的值。

    推荐文章