从 Angular 13ComponentFactoryResolver
开始已弃用。我找不到可以正确替换它的解决方案,因为rootSelectorOrNode
新解决方案中缺少旧参数。
我想RectComponent
动态创建并将其绑定到动态创建的 SVG 矩形元素。
这是我的旧解决方案:
public createRectComponent(representation:Shape<SVGRectElement>, viewContainerRef?:ViewContainerRef|null, vcrIndex?:number):ComponentRef<RectComponent>{
const rectNode:SVGRectElement = representation.element || this.renderer.createElement('rect', 'http://www.w3.org/2000/svg') as SVGRectElement;
representation.element = rectNode;
const rectComponentFactory = this.componentFactoryResolver.resolveComponentFactory(RectComponent);
viewContainerRef = viewContainerRef || this.artAccessVcr;
const rectComponentRef = rectComponentFactory.create(viewContainerRef!.injector, [], rectNode);
viewContainerRef!.insert(rectComponentRef.hostView, vcrIndex);
return rectComponentRef;
}
Run Code Online (Sandbox Code Playgroud)
新的方式应该是这样的:
...
const rectComponentRef = viewContainerRef!.createComponent(RectComponent, {
injector: viewContainerRef!.injector,
projectableNodes: [],
//rootSelectorOrNode: rectNode //This attribute is missing completly
});
...
Run Code Online (Sandbox Code Playgroud)
我在文档中找不到任何对此有帮助的信息,但我认为这不应该是一个特定的问题,因为很多第三方库提供了一个预定义的 html 节点,您可以使用它来绑定角度逻辑。这里的情况并非如此,但我仍然更愿意自己定义 svg 节点元素。
感谢您的时间和答复!
我有一个组件,其中我在一个函数中有一个请求:
getData():Observable<any>{
return this._http.get('/app/data', {observe: 'response'});
}
Run Code Online (Sandbox Code Playgroud)
它向我返回订阅中的对象数组,例如:
[{name: 'a'}, {name: 'b'}]
Run Code Online (Sandbox Code Playgroud)
或类似的东西。
我想编写一个测试,以确保getData()使用正确的url一次调用http GET,并且预订返回类似数组的内容。
我的代码:
...
it('getData() should http GET names', () => {
inject([HttpTestingController], (httpMock: HttpTestingController) => {
const names = [{name: 'a'}, {name: 'b'}];
component.getData().subscribe((res) => {
expect(res).toEqual(names);
});
const req = httpMock.expectOne('/app/data');
expect(req.request.method).toEqual("GET");
req.flush(names);
httpMock.verify();
});
});
...
Run Code Online (Sandbox Code Playgroud)
我的测试失败,并显示以下消息:“预期没有打开的请求,发现1:GET / app / data”