在观察者运行之前,将DOM.element分配给模块变量

时间:2019-05-09 09:25:47

标签: typescript polymer polymer-3.x

我有一个用打字稿编写的模块,我试图避免在$元素上使用强制转换。

我想声明一个足够类型的模块变量来保存DOM引用。

但是,我无法在元素生命周期中找到正确的位置来执行此操作。请使用我的当前代码查看下面的代码:

static get properties() {
    return {
        value: {
            type: Number,
            observer: "valueObserver"
        },
    }
}

ready() {
    // this.element = this.$.id as HTMLInputElement;
    // this will fail .... $ is undefined

    super.ready();

    this.element = this.$.id as HTMLInputElement;
    // this is useless.... the observer has already been called
}

valueObserver() {
    // I am using this:
    (<any>this.$.id).value = this.value;

    // I am trying to use instead:
    this.element.value = this.value;
    // but it will fail, this.element has not been asigned

}

0 个答案:

没有答案