如何 select 阴影根元素

how to select shadow-root elements

使用聚合物,我在 select 通过标签名称输入元素时遇到问题。

<canvas id="chart" width="150" height="150"></canvas>

.

..

...

_drawChart(price, time) { var ctx = this.$.canvas.getContext('2d'); //error happened here

.

..

...

}

现在 this.$.canvas.getContext('2d'); returns 控制台出错,Uncaught TypeError: Cannot read property 'canvas' of undefined.

我通过 getElementById 从控制台手动转到 select 元素,它返回 null,注意到 canvas 驻留在 shadow-root 树中。

尝试 this.$.chart.getContext('2d'); 而不是 this.$.canvas.getContext('2d');

this.shadowRoot.getElementById("chart").getContext('2d'); 也有效。
有点长,但更原生。