- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定用于 Dynamic Component Loading 的指令与 ViewContainerRef注入(inject):
import { Directive, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[fooHost]'
})
export class FooDirective {
constructor(public viewContainerRef: ViewContainerRef) {}
}
ViewContainerRef
的实例或模拟在单元测试中:
import { FooDirective } from './foo.directive';
describe('FooDirective', () => {
it('should create an instance', () => {
const directive = new FooDirective();
expect(directive).toBeTruthy();
});
});
An argument for 'viewContainerRef' was not provided.
ViewContainerRef
的实例。 .
@Component
与
TestBed.createComponent
并将夹具或组件实例作为
ViewContainerRef
传递?
import { FooDirective } from './foo.directive';
import { ViewContainerRef, Component } from '@angular/core';
import { TestBed, async } from '@angular/core/testing';
@Component({ selector: 'app-stub', template: '' })
class StubComponent {}
describe('LightboxDirective', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({ declarations: [StubComponent] }).compileComponents();
}));
it('should create an instance', () => {
const fixture = TestBed.createComponent(StubComponent);
const component = fixture.debugElement.componentInstance;
const directive = new FooDirective(component);
expect(directive).toBeTruthy();
});
});
ViewContainerRef
,
fixture.debugElement.componentInstance
或
fixture.debugElement.nativeElement
或者是其他东西?
最佳答案
ViewContainerRef
是从 @angular/core
导入的抽象类.因为是抽象类,所以不能直接实例化。但是,在您的测试类中,您可以简单地创建一个新类 extends
ViewContainerRef,并实现所有必需的方法。然后,您可以简单地实例化 TestViewContainerRef 的新实例,并将其传递给您的测试/规范中的 FooDirective 构造函数。像这样:
// create the test class
class TestViewContainerRef extends ViewContainerRef {
get element(): import("@angular/core").ElementRef<any> {
throw new Error("Method not implemented.");
}
get injector(): import("@angular/core").Injector {
throw new Error("Method not implemented.");
}
get parentInjector(): import("@angular/core").Injector {
throw new Error("Method not implemented.");
}
clear(): void {
throw new Error("Method not implemented.");
}
get(index: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
get length(): number {
throw new Error("Method not implemented.");
}
createEmbeddedView<C>(templateRef: import("@angular/core").TemplateRef<C>, context?: C, index?: number): import("@angular/core").EmbeddedViewRef<C> {
throw new Error("Method not implemented.");
}
createComponent<C>(componentFactory: import("@angular/core").ComponentFactory<C>, index?: number, injector?: import("@angular/core").Injector, projectableNodes?: any[][], ngModule?: import("@angular/core").NgModuleRef<any>): import("@angular/core").ComponentRef<C> {
throw new Error("Method not implemented.");
}
insert(viewRef: import("@angular/core").ViewRef, index?: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
move(viewRef: import("@angular/core").ViewRef, currentIndex: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
indexOf(viewRef: import("@angular/core").ViewRef): number {
throw new Error("Method not implemented.");
}
remove(index?: number): void {
throw new Error("Method not implemented.");
}
detach(index?: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
}
提示:我在 Mac 上使用 VS Code。当我创建类 stub
class TestViewContainerRef extends ViewContainerRef { }
, 代码给了我一个非常有用的代码提示来实现所有的抽象方法。我用它来自动生成上面的代码。其他 IDE 可能会提供类似的功能来帮助流程更顺畅。您可以在此处复制/粘贴代码以在您的测试/规范类中使用。但是,Angular 可能会随时选择更改 ViewContainerRef 抽象类的接口(interface),因此如果您确实复制了上面的代码,请注意这样做有风险。
import { ModalHostDirective } from './modal-host.directive';
import { ViewContainerRef } from '@angular/core';
class TestViewContainerRef extends ViewContainerRef {
get element(): import("@angular/core").ElementRef<any> {
throw new Error("Method not implemented.");
}
get injector(): import("@angular/core").Injector {
throw new Error("Method not implemented.");
}
get parentInjector(): import("@angular/core").Injector {
throw new Error("Method not implemented.");
}
clear(): void {
throw new Error("Method not implemented.");
}
get(index: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
get length(): number {
throw new Error("Method not implemented.");
}
createEmbeddedView<C>(templateRef: import("@angular/core").TemplateRef<C>, context?: C, index?: number): import("@angular/core").EmbeddedViewRef<C> {
throw new Error("Method not implemented.");
}
createComponent<C>(componentFactory: import("@angular/core").ComponentFactory<C>, index?: number, injector?: import("@angular/core").Injector, projectableNodes?: any[][], ngModule?: import("@angular/core").NgModuleRef<any>): import("@angular/core").ComponentRef<C> {
throw new Error("Method not implemented.");
}
insert(viewRef: import("@angular/core").ViewRef, index?: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
move(viewRef: import("@angular/core").ViewRef, currentIndex: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
indexOf(viewRef: import("@angular/core").ViewRef): number {
throw new Error("Method not implemented.");
}
remove(index?: number): void {
throw new Error("Method not implemented.");
}
detach(index?: number): import("@angular/core").ViewRef {
throw new Error("Method not implemented.");
}
}
describe('ModalHostDirective', () => {
it('should create an instance', () => {
const directive = new ModalHostDirective(new TestViewContainerRef());
expect(directive).toBeTruthy();
});
});
免责声明:就实际编写针对此 TestViewContainerRef 的测试而言,嗯……我把它留给你们所有人。但这至少满足
ng test
.
关于Angular 指令 ViewContainerRef 测试模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56828097/
假设我们有以下内容: @Directive({ selector: "[appSome]" }) export class SomeDirective { public constructor
假设我们有以下内容: @Directive({ selector: "[appSome]" }) export class SomeDirective { public constructor
有一种情况,我想在单击按钮时清除“viewContainer”,但它显示错误 ERROR TypeError: Cannot read property 'viewContainer' of unde
只是尝试阐明 ViewContainerRef.createComponent 中索引参数的含义: createComponent( componentFactory: ComponentFa
我想在 Angular 4 中创建嵌套组件 这是选择器组件 import {InputComponent} from './input/input.component' import {BlockCo
我正在尝试显示一个类似于(不完全) Angular 文档中示例的动态组件。 我有一个带有 viewContainerRef 的动态指令 @Directive({ selector: '[dyna
我正在创建一个“组件工厂”来帮助我动态创建组件。该工厂用于创建类似的组件; PopOvers、Modals、Overlays 等 它有一个 attach 方法来创建组件: attach(compone
升级到 Angular 7 后,我得到 parentInjector is deprecated: No replacement在我的代码中。由于这显然已被弃用,为什么没有可用的更新,或者 Angul
给定用于 Dynamic Component Loading 的指令与 ViewContainerRef注入(inject): import { Directive, ViewContainerRef
所以我目前正在尝试创建嵌套评论,并且我目前正在引入一个动态表单组件,以便人们可以回复评论。当只有 parent 和 child 时,它效果很好,但如果有 sibling ,它会选择第一个。例如, --
所以我目前正在尝试创建嵌套评论,并且我目前正在引入一个动态表单组件,以便人们可以回复评论。当只有 parent 和 child 时,它效果很好,但如果有 sibling ,它会选择第一个。例如, --
什么更好?使用 ngFor 或 ViewContainerRef 动态创建组件?有什么区别? 例如,如果我有一个创建新元素的按钮,每次按下它都会生成一个新组件。 1) 第一个选项如下 items: n
我有一个组件: @Component({ selector: 'vcrdi', template: ` ViewContainerRef DI `, })
我使用 viewContainerRef.createComponent 将动态组件加载到根组件中(......),但实际上它附加了错误的位置, 我的代码: -----app.compoment.ts
当我使用 ViewContainerRef 创建组件并将实例分配给负责创建子组件的父组件的属性时,是否需要将此属性设置为 null 如果我想释放内存,在调用 ViewContainerRef.clea
有没有办法从动态创建的组件中获取 ViewContainerRef?我动态创建的组件内部有一个 ngContent 元素,我想在动态创建后填充它。 export class Example { @
我是这样动态添加组件的: export class CustomersOverviewComponent implements OnInit, OnDestroy { @ViewChild(Pan
嘿,我现在正在逐步学习 Angular 2,并且我已经构建了一个名为 except 的结构自定义指令。除非行为与 ngIf act 相同,否则如果我的条件为真,则将元素附加到 dom,否则将其分离。这
我正在尝试获取 ViewContainerRef 中动态创建的组件的 index 我需要获取索引,这样我也可以销毁组件。 代码如下 @ViewChild('dynamicInsert', { read
@Component({ selector: 'my-cmp', template: ` ` }) export class MyCmp { @ViewChild('target',
我是一名优秀的程序员,十分优秀!