openLayers addFeature 从 angular 中的 xmlhttprequest 到 vectorsource

openLayers addFeature to a vectorsource from xmlhttprequest in angular

我从 https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html 他们使用

的地方得到了启发
   vectorSource.addFeatures(vectorSource.getFormat().readFeatures(xhr.responseText));

但在 angular 中使用打字稿 addFeatures 接受 Feature[] 而 vectorSource.getFormat().readFeatures(xhr.responseText) 给出 FeatureLike[].

Argument of type 'FeatureLike[] | undefined' is not assignable to parameter of type 'Feature[]'. Type 'undefined' is not assignable to type 'Feature[]'.ts(2345)

这里是错误的部分:

     xhr.onload = () => {
                    if (xhr.status === 200) {
                        source.addFeatures(
                            source
                                .getFormat()
                                ?.readFeatures(xhr.responseText)
                        );
                    } else {
                        console.log('error');
                    }
                };

有没有办法让 transform/cast FeatureLike 变成普通的 Feature,或者我做错了?

首先找到使用 GeoJSON 转换数据的解决方案(参考:How to reference another answer?

  xhr.onload = () => {
                    if (xhr.status === 200) {
                        let features = new GeoJSON({
                            featureProjection: 'EPSG:32633',
                        }).readFeatures(xhr.responseText);
                        source.addFeatures(features);
                    } else {
                        console.log('error');
                    }
                };