问题
I'm testing svg.js library with typescript and having some problems with usage of CSS selectors. I would like to select SVG element using select()
method with class selector. In the presented fiddle it works perfectly with javascript:
var circleTwo = SVG.select('circle.circle-01');
Unfortunately, it doesn't work with typescript. Lib is imported like that:
import * as SVG from 'svg.js;
When hover on select()
method, getting the info about second parameter:
However according to the documentation
Additionally, a second argument can be passed to define the parent element to search in
It seems that it is an optional argument because you can use select()
method without it:
var elements = SVG.select('rect.my-class').fill('#f06')
I've played a bit and found how to get rid of this error (the question is, is this correct approach?), however I can't use later the click()
event on the selected element, because of another error:
Property 'click' does not exist on type 'Set'.
testMe()
is the simple method to test the click()
event on the selected element:
testMe() {
alert('it works even better!');
}
What am I doing wrong?
回答1:
Seems like the type definitions are wrong. Thats not your fault, you should open a bug report. Until it gets fixed you could work around it:
SVG.select('rect.my-class', undefined as any)
来源:https://stackoverflow.com/questions/49387057/using-svg-select-method-with-class-selector-of-the-svg-js-library-doesnt-work